Skip to content

Commit d7deaa3

Browse files
authored
Merge pull request #95279 from kgremban/nov7-40520
C tutorial code improvements
2 parents 8a3d52e + 2f42389 commit d7deaa3

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

articles/iot-edge/tutorial-c-module-windows.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ The default module code receives messages on an input queue and passes them alon
132132
)
133133
```
134134
135-
3. Add **my_parson** to the list of libraries in the **target_link_libraries** section of the CMakeLists.txt file.
135+
3. Add `my_parson` to the list of libraries in the **target_link_libraries** section of the CMakeLists.txt file.
136136
137137
4. Save the **CMakeLists.txt** file.
138138
@@ -172,6 +172,14 @@ The default module code receives messages on an input queue and passes them alon
172172
4. Find the `InputQueue1Callback` function, and replace the whole function with the following code. This function implements the actual messaging filter. When a message is received, it checks whether the reported temperature exceeds the threshold. If yes, then it forwards the message through its output queue. If not, then it ignores the message.
173173

174174
```c
175+
static unsigned char *bytearray_to_str(const unsigned char *buffer, size_t len)
176+
{
177+
unsigned char *ret = (unsigned char *)malloc(len + 1);
178+
memcpy(ret, buffer, len);
179+
ret[len] = '\0';
180+
return ret;
181+
}
182+
175183
static IOTHUBMESSAGE_DISPOSITION_RESULT InputQueue1Callback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
176184
{
177185
IOTHUBMESSAGE_DISPOSITION_RESULT result;
@@ -181,7 +189,10 @@ The default module code receives messages on an input queue and passes them alon
181189
unsigned const char* messageBody;
182190
size_t contentSize;
183191

184-
if (IoTHubMessage_GetByteArray(message, &messageBody, &contentSize) != IOTHUB_MESSAGE_OK)
192+
if (IoTHubMessage_GetByteArray(message, &messageBody, &contentSize) == IOTHUB_MESSAGE_OK)
193+
{
194+
messageBody = bytearray_to_str(messageBody, contentSize);
195+
} else
185196
{
186197
messageBody = "<null>";
187198
}

articles/iot-edge/tutorial-c-module.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: shizn
77
manager: philmea
88

99
ms.author: xshi
10-
ms.date: 08/23/2019
10+
ms.date: 11/07/2019
1111
ms.topic: tutorial
1212
ms.service: iot-edge
1313
ms.custom: "mvc, seodec18"
@@ -112,7 +112,7 @@ The default module code receives messages on an input queue and passes them alon
112112
)
113113
```
114114
115-
3. Add **my_parson** to the list of libraries in the **target_link_libraries** function of CMakeLists.txt.
115+
3. Add `my_parson` to the list of libraries in the **target_link_libraries** function of CMakeLists.txt.
116116
117117
4. Save the **CMakeLists.txt** file.
118118
@@ -152,6 +152,14 @@ The default module code receives messages on an input queue and passes them alon
152152
1. Replace the entire `InputQueue1Callback` function with the following code. This function implements the actual messaging filter. When a message is received, it checks whether the reported temperature exceeds the threshold. If yes, then it forwards the message through its output queue. If not, then it ignores the message.
153153

154154
```c
155+
static unsigned char *bytearray_to_str(const unsigned char *buffer, size_t len)
156+
{
157+
unsigned char *ret = (unsigned char *)malloc(len + 1);
158+
memcpy(ret, buffer, len);
159+
ret[len] = '\0';
160+
return ret;
161+
}
162+
155163
static IOTHUBMESSAGE_DISPOSITION_RESULT InputQueue1Callback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
156164
{
157165
IOTHUBMESSAGE_DISPOSITION_RESULT result;
@@ -161,7 +169,10 @@ The default module code receives messages on an input queue and passes them alon
161169
unsigned const char* messageBody;
162170
size_t contentSize;
163171

164-
if (IoTHubMessage_GetByteArray(message, &messageBody, &contentSize) != IOTHUB_MESSAGE_OK)
172+
if (IoTHubMessage_GetByteArray(message, &messageBody, &contentSize) == IOTHUB_MESSAGE_OK)
173+
{
174+
messageBody = bytearray_to_str(messageBody, contentSize);
175+
} else
165176
{
166177
messageBody = "<null>";
167178
}

0 commit comments

Comments
 (0)