Bug 1137107 - Part 2: Add NfcResponseType and NfcNotificationType. r=smaug, dimi

This commit is contained in:
Yoshi Huang 2015-02-26 17:23:17 +08:00
parent 4cf97dac15
commit 3c9cfd2a18
7 changed files with 127 additions and 100 deletions

View File

@ -91,6 +91,32 @@ const NfcRequestType = {
TRANSCEIVE: "transceive"
};
// Should be consistent with NfcResponseType defined in NfcOptions.webidl.
const NfcResponseType = {
CHANGE_RF_STATE_RSP: "changeRFStateRsp",
READ_NDEF_RSP: "readNDEFRsp",
WRITE_NDEF_RSP: "writeNDEFRsp",
MAKE_READ_ONLY_RSP: "makeReadOnlyRsp",
FORMAT_RSP: "formatRsp",
TRANSCEIVE_RSP: "transceiveRsp",
};
const EventMsgTable = {};
EventMsgTable[NfcResponseType.CHANGE_RF_STATE_RSP] = "NFC:ChangeRFStateResponse";
EventMsgTable[NfcResponseType.READ_NDEF_RSP] = "NFC:ReadNDEFResponse";
EventMsgTable[NfcResponseType.WRITE_NDEF_RSP] = "NFC:WriteNDEFResponse";
EventMsgTable[NfcResponseType.MAKE_READ_ONLY_RSP] = "NFC:MakeReadOnlyResponse";
EventMsgTable[NfcResponseType.FORMAT_RSP] = "NFC:FormatResponse";
EventMsgTable[NfcResponseType.TRANSCEIVE_RSP] = "NFC:TransceiveResponse";
// Should be consistent with NfcNotificationType defined in NfcOptions.webidl.
const NfcNotificationType = {
INITIALIZED: "initialized",
TECH_DISCOVERED: "techDiscovered",
TECH_LOST: "techLost",
HCI_EVENT_TRANSACTION: "hciEventTransaction"
};
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
"@mozilla.org/parentprocessmessagemanager;1",
"nsIMessageBroadcaster");
@ -251,6 +277,14 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
sessionToken: sessionToken});
},
notifySendFileStatus: function notifySendFileStatus(message) {
if (message.data.status) {
message.data.errorMsg =
this.nfc.getErrorMessage(NFC.NFC_GECKO_ERROR_SEND_FILE_FAILED);
}
this.nfc.sendFileStatusResponse(message.data);
},
callDefaultFoundHandler: function callDefaultFoundHandler(message) {
let sysMsg = new NfcTechDiscoveredSysMsg(message.sessionToken,
message.isP2P,
@ -356,12 +390,7 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
case "NFC:NotifySendFileStatus":
// Upon receiving the status of sendFile operation, send the response
// to appropriate content process.
message.data.type = "NotifySendFileStatusResponse";
if (message.data.status) {
message.data.errorMsg =
this.nfc.getErrorMessage(NFC.NFC_GECKO_ERROR_SEND_FILE_FAILED);
}
this.nfc.sendNfcResponse(message.data);
this.notifySendFileStatus(message);
return null;
case "NFC:CallDefaultFoundHandler":
this.callDefaultFoundHandler(message.data);
@ -492,15 +521,33 @@ Nfc.prototype = {
this.nfcService.sendCommand(message);
},
sendNfcResponse: function sendNfcResponse(message) {
let target = this.targetsByRequestId[message.requestId];
sendFileStatusResponse: function sendFileStatusResponse(message) {
let target = this.getTargetByRequestId(message.requestId);
if (!target) {
debug("No target for requestId: " + message.requestId);
return;
}
delete this.targetsByRequestId[message.requestId];
target.sendAsyncMessage("NFC:" + message.type, message);
target.sendAsyncMessage("NFC:NotifySendFileStatusResponse", message);
},
sendNfcResponse: function sendNfcResponse(message) {
let target = this.getTargetByRequestId(message.requestId);
if (!target) {
return;
}
target.sendAsyncMessage(EventMsgTable[message.type], message);
},
getTargetByRequestId: function getTargetByRequestId(requestId) {
let target = this.targetsByRequestId[requestId];
if (!target) {
debug("No target for requestId: " + requestId);
return null;
}
delete this.targetsByRequestId[requestId];
return target;
},
/**
@ -532,12 +579,12 @@ Nfc.prototype = {
let message = Cu.cloneInto(event, this);
DEBUG && debug("Received message from NFC Service: " + JSON.stringify(message));
message.type = message.rspType || message.ntfType;
switch (message.type) {
case "InitializedNotification":
case NfcNotificationType.INITIALIZED:
// Do nothing.
break;
case "TechDiscoveredNotification":
message.type = "techDiscovered";
case NfcNotificationType.TECH_DISCOVERED:
// Update the upper layers with a session token (alias)
message.sessionToken =
SessionHelper.registerSession(message.sessionId, message.isP2P);
@ -558,9 +605,7 @@ Nfc.prototype = {
gMessageManager.onTagFound(message);
}
break;
case "TechLostNotification":
message.type = "techLost";
case NfcNotificationType.TECH_LOST:
// Update the upper layers with a session token (alias)
message.sessionToken = SessionHelper.getToken(message.sessionId);
if (SessionHelper.isP2PSession(message.sessionId)) {
@ -571,10 +616,10 @@ Nfc.prototype = {
SessionHelper.unregisterSession(message.sessionId);
break;
case "HCIEventTransactionNotification":
case NfcNotificationType.HCI_EVENT_TRANSACTION:
this.notifyHCIEventTransaction(message);
break;
case "ChangeRFStateResponse":
case NfcResponseType.CHANGE_RF_STATE_RSP:
this.sendNfcResponse(message);
if (!message.errorMsg) {
@ -582,11 +627,11 @@ Nfc.prototype = {
gMessageManager.onRFStateChanged(this.rfState);
}
break;
case "ReadNDEFResponse": // Fall through.
case "MakeReadOnlyResponse":
case "FormatResponse":
case "TransceiveResponse":
case "WriteNDEFResponse":
case NfcResponseType.READ_NDEF_RSP: // Fall through.
case NfcResponseType.WRITE_NDEF_RSP:
case NfcResponseType.MAKE_READ_ONLY_RSP:
case NfcResponseType.FORMAT_RSP:
case NfcResponseType.TRANSCEIVE_RSP:
this.sendNfcResponse(message);
break;
default:

View File

@ -10,22 +10,6 @@ namespace mozilla {
#define NFCD_MAJOR_VERSION 1
#define NFCD_MINOR_VERSION 21
enum NfcResponse {
ChangeRFStateRsp,
ReadNDEFRsp,
WriteNDEFRsp,
MakeReadOnlyRsp,
FormatRsp,
TransceiveRsp,
};
enum NfcNotification {
Initialized,
TechDiscovered,
TechLost,
HCIEventTransaction,
};
enum NfcTechlogy {
NDEF = 0,
NDEFWritable,

View File

@ -17,19 +17,6 @@ using namespace android;
using namespace mozilla;
using namespace mozilla::dom;
static const char* kChangeRFStateResponse = "ChangeRFStateResponse";
static const char* kReadNDEFResponse = "ReadNDEFResponse";
static const char* kWriteNDEFResponse = "WriteNDEFResponse";
static const char* kMakeReadOnlyResponse = "MakeReadOnlyResponse";
static const char* kFormatResponse = "FormatResponse";
static const char* kTransceiveResponse = "TransceiveResponse";
static const char* kInitializedNotification = "InitializedNotification";
static const char* kTechDiscoveredNotification = "TechDiscoveredNotification";
static const char* kTechLostNotification = "TechLostNotification";
static const char* kHCIEventTransactionNotification =
"HCIEventTransactionNotification";
bool
NfcMessageHandler::Marshall(Parcel& aParcel, const CommandOptions& aOptions)
{
@ -66,10 +53,10 @@ NfcMessageHandler::Unmarshall(const Parcel& aParcel, EventOptions& aOptions)
{
mozilla::unused << htonl(aParcel.readInt32()); // parcel size
int32_t type = aParcel.readInt32();
bool isNotification = type >> 31;
bool isNtf = type >> 31;
int32_t msgType = type & ~(1 << 31);
return isNotification ? ProcessNotification(msgType, aParcel, aOptions) :
return isNtf ? ProcessNotification(msgType, aParcel, aOptions) :
ProcessResponse(msgType, aParcel, aOptions);
}
@ -77,19 +64,20 @@ bool
NfcMessageHandler::ProcessResponse(int32_t aType, const Parcel& aParcel, EventOptions& aOptions)
{
bool result;
switch (aType) {
case NfcResponse::ChangeRFStateRsp:
aOptions.mRspType = static_cast<NfcResponseType>(aType);
switch (aOptions.mRspType) {
case NfcResponseType::ChangeRFStateRsp:
result = ChangeRFStateResponse(aParcel, aOptions);
break;
case NfcResponse::ReadNDEFRsp:
case NfcResponseType::ReadNDEFRsp:
result = ReadNDEFResponse(aParcel, aOptions);
break;
case NfcResponse::WriteNDEFRsp: // Fall through.
case NfcResponse::MakeReadOnlyRsp:
case NfcResponse::FormatRsp:
result = GeneralResponse(aType, aParcel, aOptions);
case NfcResponseType::WriteNDEFRsp: // Fall through.
case NfcResponseType::MakeReadOnlyRsp:
case NfcResponseType::FormatRsp:
result = GeneralResponse(aParcel, aOptions);
break;
case NfcResponse::TransceiveRsp:
case NfcResponseType::TransceiveRsp:
result = TransceiveResponse(aParcel, aOptions);
break;
default:
@ -103,17 +91,18 @@ bool
NfcMessageHandler::ProcessNotification(int32_t aType, const Parcel& aParcel, EventOptions& aOptions)
{
bool result;
switch (aType) {
case NfcNotification::Initialized:
aOptions.mNtfType = static_cast<NfcNotificationType>(aType);
switch (aOptions.mNtfType) {
case NfcNotificationType::Initialized:
result = InitializeNotification(aParcel, aOptions);
break;
case NfcNotification::TechDiscovered:
case NfcNotificationType::TechDiscovered:
result = TechDiscoveredNotification(aParcel, aOptions);
break;
case NfcNotification::TechLost:
case NfcNotificationType::TechLost:
result = TechLostNotification(aParcel, aOptions);
break;
case NfcNotification::HCIEventTransaction:
case NfcNotificationType::HciEventTransaction:
result = HCIEventTransactionNotification(aParcel, aOptions);
break;
default:
@ -125,25 +114,8 @@ NfcMessageHandler::ProcessNotification(int32_t aType, const Parcel& aParcel, Eve
}
bool
NfcMessageHandler::GeneralResponse(const int32_t aResponse, const Parcel& aParcel, EventOptions& aOptions)
NfcMessageHandler::GeneralResponse(const Parcel& aParcel, EventOptions& aOptions)
{
const char* type;
switch (aResponse) {
case NfcResponse::WriteNDEFRsp:
type = kWriteNDEFResponse;
break;
case NfcResponse::MakeReadOnlyRsp:
type = kMakeReadOnlyResponse;
break;
case NfcResponse::FormatRsp:
type = kFormatResponse;
break;
default:
NMH_LOG("Nfcd, unknown general aResponse %d", aResponse);
return false;
}
aOptions.mType = NS_ConvertUTF8toUTF16(type);
aOptions.mErrorCode = aParcel.readInt32();
aOptions.mSessionId = aParcel.readInt32();
@ -165,7 +137,6 @@ NfcMessageHandler::ChangeRFStateRequest(Parcel& aParcel, const CommandOptions& a
bool
NfcMessageHandler::ChangeRFStateResponse(const Parcel& aParcel, EventOptions& aOptions)
{
aOptions.mType = NS_ConvertUTF8toUTF16(kChangeRFStateResponse);
aOptions.mErrorCode = aParcel.readInt32();
NS_ENSURE_TRUE(!mRequestIdQueue.IsEmpty(), false);
aOptions.mRequestId = mRequestIdQueue[0];
@ -187,7 +158,6 @@ NfcMessageHandler::ReadNDEFRequest(Parcel& aParcel, const CommandOptions& aOptio
bool
NfcMessageHandler::ReadNDEFResponse(const Parcel& aParcel, EventOptions& aOptions)
{
aOptions.mType = NS_ConvertUTF8toUTF16(kReadNDEFResponse);
aOptions.mErrorCode = aParcel.readInt32();
aOptions.mSessionId = aParcel.readInt32();
@ -222,7 +192,6 @@ NfcMessageHandler::TransceiveRequest(Parcel& aParcel, const CommandOptions& aOpt
bool
NfcMessageHandler::TransceiveResponse(const Parcel& aParcel, EventOptions& aOptions)
{
aOptions.mType = NS_ConvertUTF8toUTF16(kTransceiveResponse);
aOptions.mErrorCode = aParcel.readInt32();
aOptions.mSessionId = aParcel.readInt32();
@ -269,7 +238,6 @@ NfcMessageHandler::FormatRequest(Parcel& aParcel, const CommandOptions& aOptions
bool
NfcMessageHandler::InitializeNotification(const Parcel& aParcel, EventOptions& aOptions)
{
aOptions.mType = NS_ConvertUTF8toUTF16(kInitializedNotification);
aOptions.mStatus = aParcel.readInt32();
aOptions.mMajorVersion = aParcel.readInt32();
aOptions.mMinorVersion = aParcel.readInt32();
@ -286,7 +254,6 @@ NfcMessageHandler::InitializeNotification(const Parcel& aParcel, EventOptions& a
bool
NfcMessageHandler::TechDiscoveredNotification(const Parcel& aParcel, EventOptions& aOptions)
{
aOptions.mType = NS_ConvertUTF8toUTF16(kTechDiscoveredNotification);
aOptions.mSessionId = aParcel.readInt32();
aOptions.mIsP2P = aParcel.readInt32();
@ -317,7 +284,6 @@ NfcMessageHandler::TechDiscoveredNotification(const Parcel& aParcel, EventOption
bool
NfcMessageHandler::TechLostNotification(const Parcel& aParcel, EventOptions& aOptions)
{
aOptions.mType = NS_ConvertUTF8toUTF16(kTechLostNotification);
aOptions.mSessionId = aParcel.readInt32();
return true;
}
@ -325,8 +291,6 @@ NfcMessageHandler::TechLostNotification(const Parcel& aParcel, EventOptions& aOp
bool
NfcMessageHandler::HCIEventTransactionNotification(const Parcel& aParcel, EventOptions& aOptions)
{
aOptions.mType = NS_ConvertUTF8toUTF16(kHCIEventTransactionNotification);
aOptions.mOriginType = aParcel.readInt32();
aOptions.mOriginIndex = aParcel.readInt32();

View File

@ -25,7 +25,7 @@ public:
private:
bool ProcessResponse(int32_t aType, const android::Parcel& aParcel, EventOptions& aOptions);
bool GeneralResponse(int32_t aResponse, const android::Parcel& aParcel, EventOptions& aOptions);
bool GeneralResponse(const android::Parcel& aParcel, EventOptions& aOptions);
bool ChangeRFStateRequest(android::Parcel& aParcel, const CommandOptions& options);
bool ChangeRFStateResponse(const android::Parcel& aParcel, EventOptions& aOptions);
bool ReadNDEFRequest(android::Parcel& aParcel, const CommandOptions& options);

View File

@ -97,13 +97,16 @@ struct CommandOptions
struct EventOptions
{
EventOptions()
: mType(EmptyString()), mStatus(-1), mErrorCode(-1), mSessionId(-1), mRequestId(EmptyString()),
: mRspType(dom::NfcResponseType::EndGuard_),
mNtfType(dom::NfcNotificationType::EndGuard_),
mStatus(-1), mErrorCode(-1), mSessionId(-1), mRequestId(EmptyString()),
mMajorVersion(-1), mMinorVersion(-1), mIsP2P(-1),
mTagType(-1), mMaxNDEFSize(-1), mIsReadOnly(-1), mIsFormatable(-1), mRfState(-1),
mOriginType(-1), mOriginIndex(-1)
{}
nsString mType;
dom::NfcResponseType mRspType;
dom::NfcNotificationType mNtfType;
int32_t mStatus;
int32_t mErrorCode;
int32_t mSessionId;

View File

@ -98,7 +98,15 @@ public:
event.prop.Value() = mEvent.prop; \
}
COPY_FIELD(mType)
COPY_OPT_FIELD(mRspType, NfcResponseType::EndGuard_)
COPY_OPT_FIELD(mNtfType, NfcNotificationType::EndGuard_)
// Only one of rspType and ntfType should be used.
MOZ_ASSERT(((mEvent.mRspType != NfcResponseType::EndGuard_) ||
(mEvent.mNtfType != NfcNotificationType::EndGuard_)) &&
((mEvent.mRspType == NfcResponseType::EndGuard_) ||
(mEvent.mNtfType == NfcNotificationType::EndGuard_)));
COPY_OPT_FIELD(mRequestId, EmptyString())
COPY_OPT_FIELD(mStatus, -1)
COPY_OPT_FIELD(mSessionId, -1)

View File

@ -20,6 +20,28 @@ enum NfcRequestType {
"transceive"
};
/**
* Type of the Response used in NfcEventOptions.
*/
enum NfcResponseType {
"changeRFStateRsp",
"readNDEFRsp",
"writeNDEFRsp",
"makeReadOnlyRsp",
"formatRsp",
"transceiveRsp",
};
/**
* Type of the Notification used in NfcEventOptions.
*/
enum NfcNotificationType {
"initialized",
"techDiscovered",
"techLost",
"hciEventTransaction"
};
dictionary NfcCommandOptions
{
required NfcRequestType type;
@ -40,7 +62,8 @@ dictionary NfcCommandOptions
dictionary NfcEventOptions
{
DOMString type = "";
NfcResponseType rspType;
NfcNotificationType ntfType;
long status;
NfcErrorMessage errorMsg;