From ad3ea71c59067ee5a61ac860396996e9cc734ec9 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 8 Oct 2018 09:23:13 -0700 Subject: [PATCH] [posix-app] rename global variable to `gNodeId` (#3118) This commit renames `NODE_ID` to `gNodeId` in posix app and posix platform example code. It also ensures to use correct encoding (byte order) to calculate the `gNodeId` (as `uint64_t`) in posix app. --- examples/platforms/posix/flash.c | 2 +- examples/platforms/posix/logging.c | 2 +- examples/platforms/posix/platform-posix.h | 13 ++++++------- examples/platforms/posix/radio.c | 12 ++++++------ examples/platforms/posix/random.c | 4 ++-- examples/platforms/posix/sim/platform-sim.c | 11 +++++------ examples/platforms/posix/sim/radio-sim.c | 8 ++++---- examples/platforms/posix/system.c | 7 +++---- src/posix/platform/flash.c | 2 +- src/posix/platform/logging.c | 2 +- src/posix/platform/platform-posix.h | 8 +------- src/posix/platform/radio_spinel.cpp | 5 ++++- src/posix/platform/random.c | 4 ++-- src/posix/platform/sim.c | 7 ++++--- src/posix/platform/system.c | 2 +- 15 files changed, 42 insertions(+), 47 deletions(-) diff --git a/examples/platforms/posix/flash.c b/examples/platforms/posix/flash.c index eb6ac91e8..d1a712095 100644 --- a/examples/platforms/posix/flash.c +++ b/examples/platforms/posix/flash.c @@ -71,7 +71,7 @@ otError utilsFlashInit(void) offset = "0"; } - snprintf(fileName, sizeof(fileName), "%s/%s_%d.flash", path, offset, NODE_ID); + snprintf(fileName, sizeof(fileName), "%s/%s_%d.flash", path, offset, gNodeId); if (access(fileName, 0)) { diff --git a/examples/platforms/posix/logging.c b/examples/platforms/posix/logging.c index 777945ad8..7a32aa1bd 100644 --- a/examples/platforms/posix/logging.c +++ b/examples/platforms/posix/logging.c @@ -65,7 +65,7 @@ OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const offset = 0; - LOG_PRINTF("[%d] ", NODE_ID); + LOG_PRINTF("[%d] ", gNodeId); va_start(args, aFormat); charsWritten = vsnprintf(&logString[offset], sizeof(logString) - offset, aFormat, args); diff --git a/examples/platforms/posix/platform-posix.h b/examples/platforms/posix/platform-posix.h index d2caa0918..aafba12cb 100644 --- a/examples/platforms/posix/platform-posix.h +++ b/examples/platforms/posix/platform-posix.h @@ -102,17 +102,16 @@ struct Event uint8_t mData[OT_EVENT_DATA_MAX_SIZE]; } OT_TOOL_PACKED_END; +enum +{ + WELLKNOWN_NODE_ID = 34, ///< Well-known Unique ID used by a simulated radio that supports promiscuous mode. +}; + /** * Unique node ID. * */ -extern uint32_t NODE_ID; - -/** - * Well-known Unique ID used by a simulated radio that supports promiscuous mode. - * - */ -extern uint32_t WELLKNOWN_NODE_ID; +extern uint32_t gNodeId; /** * This function initializes the alarm service used by OpenThread. diff --git a/examples/platforms/posix/radio.c b/examples/platforms/posix/radio.c index d510612d3..5f7494b02 100644 --- a/examples/platforms/posix/radio.c +++ b/examples/platforms/posix/radio.c @@ -345,10 +345,10 @@ void otPlatRadioGetIeeeEui64(otInstance *aInstance, uint8_t *aIeeeEui64) aIeeeEui64[1] = 0xb4; aIeeeEui64[2] = 0x30; aIeeeEui64[3] = 0x00; - aIeeeEui64[4] = (NODE_ID >> 24) & 0xff; - aIeeeEui64[5] = (NODE_ID >> 16) & 0xff; - aIeeeEui64[6] = (NODE_ID >> 8) & 0xff; - aIeeeEui64[7] = NODE_ID & 0xff; + aIeeeEui64[4] = (gNodeId >> 24) & 0xff; + aIeeeEui64[5] = (gNodeId >> 16) & 0xff; + aIeeeEui64[6] = (gNodeId >> 8) & 0xff; + aIeeeEui64[7] = gNodeId & 0xff; } void otPlatRadioSetPanId(otInstance *aInstance, uint16_t panid) @@ -409,7 +409,7 @@ void platformRadioInit(void) } else { - sockaddr.sin_port = htons(9000 + sPortOffset + NODE_ID); + sockaddr.sin_port = htons(9000 + sPortOffset + gNodeId); } sockaddr.sin_addr.s_addr = INADDR_ANY; @@ -728,7 +728,7 @@ void radioTransmit(struct RadioMessage *aMessage, const struct otRadioFrame *aFr { ssize_t rval; - if (NODE_ID == i) + if (gNodeId == i) { continue; } diff --git a/examples/platforms/posix/random.c b/examples/platforms/posix/random.c index 4ae38247b..f003338e0 100644 --- a/examples/platforms/posix/random.c +++ b/examples/platforms/posix/random.c @@ -54,8 +54,8 @@ void platformRandomInit(void) #else // __SANITIZE_ADDRESS__ - // Multiplying NODE_ID assures that no two nodes gets the same seed within an hour. - sState = (uint32_t)time(NULL) + (3600 * NODE_ID); + // Multiplying gNodeId assures that no two nodes gets the same seed within an hour. + sState = (uint32_t)time(NULL) + (3600 * gNodeId); #endif // __SANITIZE_ADDRESS__ } diff --git a/examples/platforms/posix/sim/platform-sim.c b/examples/platforms/posix/sim/platform-sim.c index 58bfcf6f5..73ba8a2a9 100644 --- a/examples/platforms/posix/sim/platform-sim.c +++ b/examples/platforms/posix/sim/platform-sim.c @@ -50,8 +50,7 @@ #include #include -uint32_t NODE_ID = 1; -uint32_t WELLKNOWN_NODE_ID = 34; +uint32_t gNodeId = 1; extern bool gPlatformPseudoResetWasRequested; static volatile bool gTerminate = false; @@ -191,7 +190,7 @@ static void socket_init(void) sPortOffset *= WELLKNOWN_NODE_ID; } - sockaddr.sin_port = htons(9000 + sPortOffset + NODE_ID); + sockaddr.sin_port = htons(9000 + sPortOffset + gNodeId); sockaddr.sin_addr.s_addr = INADDR_ANY; sSockFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -230,11 +229,11 @@ void otSysInit(int argc, char *argv[]) gArgumentsCount = argc; gArguments = argv; - NODE_ID = (uint32_t)strtol(argv[1], &endptr, 0); + gNodeId = (uint32_t)strtol(argv[1], &endptr, 0); - if (*endptr != '\0' || NODE_ID < 1 || NODE_ID >= WELLKNOWN_NODE_ID) + if (*endptr != '\0' || gNodeId < 1 || gNodeId >= WELLKNOWN_NODE_ID) { - fprintf(stderr, "Invalid NODE_ID: %s\n", argv[1]); + fprintf(stderr, "Invalid NodeId: %s\n", argv[1]); exit(EXIT_FAILURE); } diff --git a/examples/platforms/posix/sim/radio-sim.c b/examples/platforms/posix/sim/radio-sim.c index 2c00a95ee..04d863e4d 100644 --- a/examples/platforms/posix/sim/radio-sim.c +++ b/examples/platforms/posix/sim/radio-sim.c @@ -333,10 +333,10 @@ void otPlatRadioGetIeeeEui64(otInstance *aInstance, uint8_t *aIeeeEui64) aIeeeEui64[1] = 0xb4; aIeeeEui64[2] = 0x30; aIeeeEui64[3] = 0x00; - aIeeeEui64[4] = (NODE_ID >> 24) & 0xff; - aIeeeEui64[5] = (NODE_ID >> 16) & 0xff; - aIeeeEui64[6] = (NODE_ID >> 8) & 0xff; - aIeeeEui64[7] = NODE_ID & 0xff; + aIeeeEui64[4] = (gNodeId >> 24) & 0xff; + aIeeeEui64[5] = (gNodeId >> 16) & 0xff; + aIeeeEui64[6] = (gNodeId >> 8) & 0xff; + aIeeeEui64[7] = gNodeId & 0xff; } void otPlatRadioSetPanId(otInstance *aInstance, uint16_t panid) diff --git a/examples/platforms/posix/system.c b/examples/platforms/posix/system.c index 1a3c9a66b..1efed5e2c 100644 --- a/examples/platforms/posix/system.c +++ b/examples/platforms/posix/system.c @@ -51,8 +51,7 @@ #include #include -uint32_t NODE_ID = 1; -uint32_t WELLKNOWN_NODE_ID = 34; +uint32_t gNodeId = 1; extern bool gPlatformPseudoResetWasRequested; @@ -91,9 +90,9 @@ void otSysInit(int aArgCount, char *aArgVector[]) signal(SIGHUP, &handleSignal); #endif - NODE_ID = (uint32_t)strtol(aArgVector[1], &endptr, 0); + gNodeId = (uint32_t)strtol(aArgVector[1], &endptr, 0); - if (*endptr != '\0' || NODE_ID < 1 || NODE_ID >= WELLKNOWN_NODE_ID) + if (*endptr != '\0' || gNodeId < 1 || gNodeId >= WELLKNOWN_NODE_ID) { fprintf(stderr, "Invalid NodeId: %s\n", aArgVector[1]); exit(EXIT_FAILURE); diff --git a/src/posix/platform/flash.c b/src/posix/platform/flash.c index 3f415d203..f7aceb2e8 100644 --- a/src/posix/platform/flash.c +++ b/src/posix/platform/flash.c @@ -73,7 +73,7 @@ otError utilsFlashInit(void) offset = "0"; } - snprintf(fileName, sizeof(fileName), "%s/%s_%" PRIx64 ".flash", path, offset, NODE_ID); + snprintf(fileName, sizeof(fileName), "%s/%s_%" PRIx64 ".flash", path, offset, gNodeId); if (access(fileName, 0)) { diff --git a/src/posix/platform/logging.c b/src/posix/platform/logging.c index f4fe6d590..e9696e00e 100644 --- a/src/posix/platform/logging.c +++ b/src/posix/platform/logging.c @@ -67,7 +67,7 @@ OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const offset = 0; - charsWritten = snprintf(&logString[offset], sizeof(logString), "[%" PRIx64 "] ", NODE_ID); + charsWritten = snprintf(&logString[offset], sizeof(logString), "[%" PRIx64 "] ", gNodeId); otEXPECT_ACTION(charsWritten >= 0, logString[offset] = 0); offset += (unsigned int)charsWritten; otEXPECT_ACTION(offset < sizeof(logString), logString[sizeof(logString) - 1] = 0); diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index bfbefc16a..35e312ec0 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -84,13 +84,7 @@ struct Event * Unique node ID. * */ -extern uint64_t NODE_ID; - -/** - * Well-known Unique ID used by a simulated radio that supports promiscuous mode. - * - */ -#define WELLKNOWN_NODE_ID 34 +extern uint64_t gNodeId; /** * This function initializes the alarm service used by OpenThread. diff --git a/src/posix/platform/radio_spinel.cpp b/src/posix/platform/radio_spinel.cpp index 16ab95e7b..29f217223 100644 --- a/src/posix/platform/radio_spinel.cpp +++ b/src/posix/platform/radio_spinel.cpp @@ -55,6 +55,7 @@ #include #include +#include #include #include #include @@ -402,7 +403,9 @@ void RadioSpinel::Init(const char *aRadioFile, const char *aRadioConfig) SuccessOrExit(error = WaitResponse()); VerifyOrExit(mIsReady, error = OT_ERROR_FAILED); - SuccessOrExit(error = Get(SPINEL_PROP_HWADDR, SPINEL_DATATYPE_UINT64_S, &NODE_ID)); + + SuccessOrExit(error = Get(SPINEL_PROP_HWADDR, SPINEL_DATATYPE_UINT64_S, &gNodeId)); + gNodeId = ot::Encoding::BigEndian::HostSwap64(gNodeId); { unsigned int caps; diff --git a/src/posix/platform/random.c b/src/posix/platform/random.c index a708c198d..b29be5a4a 100644 --- a/src/posix/platform/random.c +++ b/src/posix/platform/random.c @@ -54,8 +54,8 @@ void platformRandomInit(void) #else // __SANITIZE_ADDRESS__ - // Multiplying NODE_ID assures that no two nodes gets the same seed within an hour. - sState = (uint32_t)time(NULL) + (3600 * NODE_ID); + // Multiplying gNodeId assures that no two nodes gets the same seed within an hour. + sState = (uint32_t)time(NULL) + (3600 * gNodeId); #endif // __SANITIZE_ADDRESS__ } diff --git a/src/posix/platform/sim.c b/src/posix/platform/sim.c index fd9fc4677..ee1194760 100644 --- a/src/posix/platform/sim.c +++ b/src/posix/platform/sim.c @@ -39,8 +39,9 @@ #if OPENTHREAD_POSIX_VIRTUAL_TIME -static const int kBasePort = 18000; ///< This base port for posix app simulation. -static const int kUsPerSecond = 1000000; ///< Number of microseconds per second. +static const int kWellKnownNodeId = 34; ///< Well-known ID used by a simulated radio supporting promiscuous mode. +static const int kBasePort = 18000; ///< This base port for posix app simulation. +static const int kUsPerSecond = 1000000; ///< Number of microseconds per second. static uint64_t sNow = 0; ///< Time of simulation. static int sSockFd = -1; ///< Socket used to communicating with simulator. @@ -69,7 +70,7 @@ void otSimInit(void) exit(EXIT_FAILURE); } - sPortOffset *= WELLKNOWN_NODE_ID; + sPortOffset *= kWellKnownNodeId; } // node id is required for virtual time simulation diff --git a/src/posix/platform/system.c b/src/posix/platform/system.c index 00d5b9e9c..b37a9833a 100644 --- a/src/posix/platform/system.c +++ b/src/posix/platform/system.c @@ -45,7 +45,7 @@ #include #include -uint64_t NODE_ID = 0; +uint64_t gNodeId = 0; extern bool gPlatformPseudoResetWasRequested;