forked from Azure/iot-edge-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_world.c
212 lines (192 loc) · 5.73 KB
/
hello_world.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <stdlib.h>
#include "module.h"
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/threadapi.h"
#include "hello_world.h"
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/lock.h"
typedef struct HELLOWORLD_HANDLE_DATA_TAG
{
THREAD_HANDLE threadHandle;
LOCK_HANDLE lockHandle;
int stopThread;
BROKER_HANDLE broker;
}HELLOWORLD_HANDLE_DATA;
#define HELLOWORLD_MESSAGE "hello world"
int helloWorldThread(void *param)
{
HELLOWORLD_HANDLE_DATA* handleData = param;
MESSAGE_CONFIG msgConfig;
MAP_HANDLE propertiesMap = Map_Create(NULL);
if(propertiesMap == NULL)
{
LogError("unable to create a Map");
}
else
{
if (Map_AddOrUpdate(propertiesMap, "helloWorld", "from Azure IoT Gateway SDK simple sample!") != MAP_OK)
{
LogError("unable to Map_AddOrUpdate");
}
else
{
msgConfig.size = (size_t)strlen(HELLOWORLD_MESSAGE);
msgConfig.source = (unsigned char*)HELLOWORLD_MESSAGE;
msgConfig.sourceProperties = propertiesMap;
MESSAGE_HANDLE helloWorldMessage = Message_Create(&msgConfig);
if (helloWorldMessage == NULL)
{
LogError("unable to create \"hello world\" message");
}
else
{
while (1)
{
if (Lock(handleData->lockHandle) == LOCK_OK)
{
if (handleData->stopThread)
{
(void)Unlock(handleData->lockHandle);
break; /*gets out of the thread*/
}
else
{
(void)Broker_Publish(handleData->broker, (MODULE_HANDLE)handleData, helloWorldMessage);
(void)Unlock(handleData->lockHandle);
}
}
else
{
/*shall retry*/
}
(void)ThreadAPI_Sleep(5000); /*every 5 seconds*/
}
Message_Destroy(helloWorldMessage);
}
}
}
return 0;
}
static MODULE_HANDLE HelloWorld_Create(BROKER_HANDLE broker, const void* configuration)
{
HELLOWORLD_HANDLE_DATA* result;
(void)configuration;
if (broker == NULL) /*configuration is not used*/
{
LogError("invalid arg broker=%p", broker);
result = NULL;
}
else
{
result = malloc(sizeof(HELLOWORLD_HANDLE_DATA));
if(result == NULL)
{
LogError("unable to malloc");
}
else
{
result->lockHandle = Lock_Init();
if(result->lockHandle == NULL)
{
LogError("unable to Lock_Init");
free(result);
result = NULL;
}
else
{
result->stopThread = 0;
result->broker = broker;
result->threadHandle = NULL;
}
}
}
return result;
}
static void* HelloWorld_ParseConfigurationFromJson(const char* configuration)
{
(void)configuration;
return NULL;
}
static void HelloWorld_FreeConfiguration(void* configuration)
{
(void)configuration;
}
static void HelloWorld_Start(MODULE_HANDLE module)
{
HELLOWORLD_HANDLE_DATA* handleData = module;
if (handleData != NULL)
{
if (Lock(handleData->lockHandle) != LOCK_OK)
{
LogError("not able to Lock, still setting the thread to finish");
handleData->stopThread = 1;
}
else
{
if (ThreadAPI_Create(&handleData->threadHandle, helloWorldThread, handleData) != THREADAPI_OK)
{
LogError("failed to spawn a thread");
handleData->threadHandle = NULL;
}
(void)Unlock(handleData->lockHandle);
}
}
}
static void HelloWorld_Destroy(MODULE_HANDLE module)
{
/*first stop the thread*/
HELLOWORLD_HANDLE_DATA* handleData = module;
int notUsed;
if (Lock(handleData->lockHandle) != LOCK_OK)
{
LogError("not able to Lock, still setting the thread to finish");
handleData->stopThread = 1;
}
else
{
handleData->stopThread = 1;
Unlock(handleData->lockHandle);
}
if (handleData->threadHandle != NULL &&
ThreadAPI_Join(handleData->threadHandle, ¬Used) != THREADAPI_OK)
{
LogError("unable to ThreadAPI_Join, still proceeding in _Destroy");
}
(void)Lock_Deinit(handleData->lockHandle);
free(handleData);
}
static void HelloWorld_Receive(MODULE_HANDLE moduleHandle, MESSAGE_HANDLE messageHandle)
{
/*no action, HelloWorld is not interested in any messages*/
(void)moduleHandle;
(void)messageHandle;
}
static const MODULE_API_1 HelloWorld_API_all =
{
{MODULE_API_VERSION_1},
HelloWorld_ParseConfigurationFromJson,
HelloWorld_FreeConfiguration,
HelloWorld_Create,
HelloWorld_Destroy,
HelloWorld_Receive,
HelloWorld_Start
};
#ifdef BUILD_MODULE_TYPE_STATIC
MODULE_EXPORT const MODULE_API* MODULE_STATIC_GETAPI(HELLOWORLD_MODULE)(MODULE_API_VERSION gateway_api_version)
#else
MODULE_EXPORT const MODULE_API* Module_GetApi(MODULE_API_VERSION gateway_api_version)
#endif
{
const MODULE_API * api;
if (gateway_api_version >= HelloWorld_API_all.base.version)
{
api= (const MODULE_API*)&HelloWorld_API_all;
}
else
{
api = NULL;
}
return api;
}