1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Cloud Helper                                                        */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
27 /*                                                                        */
28 /*    nx_cloud.h                                          PORTABLE C      */
29 /*                                                           6.1          */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Yuxin Zhou, Microsoft Corporation                                   */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file defines the NetX Cloud component, including all data      */
37 /*    types and external references.                                      */
38 /*                                                                        */
39 /*  RELEASE HISTORY                                                       */
40 /*                                                                        */
41 /*    DATE              NAME                      DESCRIPTION             */
42 /*                                                                        */
43 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
44 /*                                                                        */
45 /**************************************************************************/
46 
47 #ifndef NX_CLOUD_H
48 #define NX_CLOUD_H
49 
50 /* Determine if a C++ compiler is being used.  If so, ensure that standard
51    C is used to process the API information.  */
52 
53 #ifdef __cplusplus
54 
55    /* Yes, C++ compiler is present.  Use standard C.  */
56 extern   "C" {
57 
58 #endif
59 
60 /* Include the ThreadX and port-specific data type file.  */
61 
62 #include "tx_api.h"
63 #include "nx_api.h"
64 
65 
66 /* Define CLOUD constants.  */
67 #define NX_CLOUD_ID                                     ((ULONG)0x434C4F44)
68 
69 
70 /* API return values.  */
71 #define NX_CLOUD_MODULE_ALREADY_REGISTERED              0xF0
72 #define NX_CLOUD_MODULE_NOT_REGISTERED                  0xF1
73 #define NX_CLOUD_MODULE_BOUND                           0xF2
74 #define NX_CLOUD_MODULE_EVENT_INVALID                   0xF3
75 
76 
77 /* Define events processed by cloud helper. */
78 #define NX_CLOUD_ALL_EVENTS                             0xFFFFFFFF      /* All event flags.                */
79 
80 /* Define the common events for all modules.  */
81 #define NX_CLOUD_COMMON_PERIODIC_EVENT                  0x00000001u     /* Periodic event, 1s              */
82 
83 /* Define the module events.  */
84 #define NX_CLOUD_MODULE_MQTT_EVENT                      0x00010000u     /* MQTT event                      */
85 #define NX_CLOUD_MODULE_AZURE_SDK_EVENT                 0x00020000u     /* Azure SDK event                 */
86 #define NX_CLOUD_MODULE_AZURE_ADU_EVENT                 0x00040000u     /* Azure Device Update event       */
87 #define NX_CLOUD_MODULE_AZURE_ISM_EVENT                 0x00080000u     /* Azure IoT Security Module event */
88 
89 
90 /* Define the all common events.  */
91 #define NX_CLOUD_COMMON_ALL_EVENT                      (NX_CLOUD_COMMON_PERIODIC_EVENT)
92 
93 
94 typedef struct NX_CLOUD_MODULE_STRUCT
95 {
96 
97     /* Define the module name.  */
98     const CHAR                     *nx_cloud_module_name;
99 
100     /* Define the module registered events including common events and module event
101        that are processed in cloud helper thread.  */
102     ULONG                           nx_cloud_module_registered_events;
103 
104     /* Define the actual current event flags in this module that are processed
105        in module processing routine.  */
106     ULONG                           nx_cloud_module_own_events;
107 
108     /* Define the module processing routine.  */
109     VOID                          (*nx_cloud_module_process)(VOID *module_context, ULONG common_events, ULONG module_own_events);
110 
111     /* Define the context that is passed to module processing routine.  */
112     VOID                           *nx_cloud_module_context;
113 
114     /* Define the next pointer of created module.  */
115     struct NX_CLOUD_MODULE_STRUCT  *nx_cloud_module_next;
116 
117     /* Define the cloud pointer associated with the module.  */
118     struct NX_CLOUD_STRUCT         *nx_cloud_ptr;
119 
120 } NX_CLOUD_MODULE;
121 
122 typedef struct NX_CLOUD_STRUCT
123 {
124 
125     /* Define the cloud ID.  */
126     ULONG                           nx_cloud_id;
127 
128     /* Define the cloud name.  */
129     const CHAR                     *nx_cloud_name;
130 
131     /* Define the cloud helper thread that process cloud modules.  */
132     TX_THREAD                       nx_cloud_thread;
133 
134     /* Define the event flags that are used to stimulate the cloud helper
135        thread.  */
136     TX_EVENT_FLAGS_GROUP            nx_cloud_events;
137 
138     /* Define the internal mutex used for protection .  */
139     TX_MUTEX                        nx_cloud_mutex;
140 
141     /* Define the periodic timer for cloud modules.  */
142     TX_TIMER                        nx_cloud_periodic_timer;
143 
144     /* Define the head pointer of the created module list.  */
145     NX_CLOUD_MODULE                *nx_cloud_modules_list_header;
146 
147     /* Define the number of created module instances.  */
148     ULONG                           nx_cloud_modules_count;
149 
150 } NX_CLOUD;
151 
152 
153 #ifndef NX_CLOUD_SOURCE_CODE
154 
155 /* Application caller is present, perform API mapping.  */
156 
157 /* Determine if error checking is desired.  If so, map CLOUD API functions
158    to the appropriate error checking front-ends.  Otherwise, map API
159    functions to the core functions that actually perform the work.
160    Note: error checking is enabled by default.  */
161 
162 #ifdef NX_DISABLE_ERROR_CHECKING
163 
164 /* Services without error checking.  */
165 
166 #define nx_cloud_create                                 _nx_cloud_create
167 #define nx_cloud_delete                                 _nx_cloud_delete
168 #define nx_cloud_module_register                        _nx_cloud_module_register
169 #define nx_cloud_module_deregister                      _nx_cloud_module_deregister
170 #define nx_cloud_module_event_set                       _nx_cloud_module_event_set
171 #define nx_cloud_module_event_clear                     _nx_cloud_module_event_clear
172 
173 #else
174 
175 /* Services with error checking.  */
176 
177 #define nx_cloud_create                                 _nxe_cloud_create
178 #define nx_cloud_delete                                 _nxe_cloud_delete
179 #define nx_cloud_module_register                        _nxe_cloud_module_register
180 #define nx_cloud_module_deregister                      _nxe_cloud_module_deregister
181 #define nx_cloud_module_event_set                       _nxe_cloud_module_event_set
182 #define nx_cloud_module_event_clear                     _nxe_cloud_module_event_clear
183 
184 #endif
185 
186 /* Define the prototypes accessible to the application software.  */
187 
188 /* Create/delete cloud helper thread.  */
189 UINT nx_cloud_create(NX_CLOUD* cloud_ptr, const CHAR* cloud_name, VOID* memory_ptr, ULONG memory_size, UINT priority);
190 UINT nx_cloud_delete(NX_CLOUD* cloud_ptr);
191 
192 /* Register/deregister module in cloud thread.  */
193 UINT nx_cloud_module_register(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr, const CHAR* module_name, ULONG module_event,
194                               VOID (*module_process)(VOID* module_context, ULONG common_events, ULONG module_own_events), VOID* module_context);
195 UINT nx_cloud_module_deregister(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr);
196 UINT nx_cloud_module_event_set(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event);
197 UINT nx_cloud_module_event_clear(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event);
198 
199 #else
200 
201 /* Cloud source code is being compiled, do not perform any API mapping.  */
202 
203 UINT _nxe_cloud_create(NX_CLOUD* cloud_ptr, const CHAR* cloud_name, VOID* memory_ptr, ULONG memory_size, UINT priority);
204 UINT _nx_cloud_create(NX_CLOUD* cloud_ptr, const CHAR* cloud_name, VOID* memory_ptr, ULONG memory_size, UINT priority);
205 UINT _nxe_cloud_delete(NX_CLOUD* cloud_ptr);
206 UINT _nx_cloud_delete(NX_CLOUD* cloud_ptr);
207 UINT _nxe_cloud_module_register(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr, const CHAR* module_name, ULONG module_event,
208                                 VOID(*module_process)(VOID* module_context, ULONG common_events, ULONG module_own_events), VOID* module_context);
209 UINT _nx_cloud_module_register(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr, const CHAR* module_name, ULONG module_event,
210                                VOID(*module_process)(VOID* module_context, ULONG common_events, ULONG module_own_events), VOID* module_context);
211 UINT _nxe_cloud_module_deregister(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr);
212 UINT _nx_cloud_module_deregister(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr);
213 UINT _nxe_cloud_module_event_set(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event);
214 UINT _nx_cloud_module_event_set(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event);
215 UINT _nxe_cloud_module_event_clear(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event);
216 UINT _nx_cloud_module_event_clear(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event);
217 
218 #endif
219 
220 
221 /* Determine if a C++ compiler is being used.  If so, complete the standard
222    C conditional started above.  */
223 #ifdef __cplusplus
224 }
225 #endif
226 
227 #endif /* NX_CLOUD_H  */
228