1+ #include < ArduinoJson.h>
2+
3+ #include < AzureIoTHub.h>
4+ #include < SPIFFS.h>
5+
6+ #include " logging.h"
7+ #include " iot_device_core.h"
8+ #include " iot_device_method.h"
9+
10+ struct MethodCallback {
11+ const char * methodName;
12+ IOT_DEVICE_METHOD_CALLBACK callback;
13+ };
14+
15+ static MethodCallback methodCallbacks[IOT_DEVICE_MAX_METHOD_COUNT ];
16+ static int registeredCount = 0 ;
17+
18+ static int IoTDevice_DeviceMethodCallback (const char * method_name, const unsigned char * payload, size_t size, unsigned char ** response, size_t * response_size, void * userContextCallback)
19+ {
20+ (void )userContextCallback;
21+ (void )payload;
22+ (void )size;
23+
24+ bool methodFound = false ;
25+
26+ int result;
27+
28+ Log_Debug (" Direct method lookup: %s" , method_name);
29+
30+ for (int i = 0 ; i < registeredCount; i++)
31+ {
32+ Log_Trace (" Trying %s" , methodCallbacks[i].methodName );
33+
34+ if (strcmp (methodCallbacks[i].methodName , method_name) == 0 )
35+ {
36+ Log_Debug (" Method callback for %s found. Executing" , method_name);
37+
38+ methodFound = true ;
39+ result = methodCallbacks[i].callback (payload, size, response, response_size);
40+ }
41+ }
42+
43+ if (!methodFound)
44+ {
45+ Log_Info (" Method %s not registered!" , method_name);
46+ const char deviceMethodResponse[] = " { }" ;
47+ *response_size = sizeof (deviceMethodResponse)-1 ;
48+ *response = (unsigned char *)malloc (*response_size);
49+ (void )memcpy (*response, deviceMethodResponse, *response_size);
50+ result = -1 ;
51+ }
52+
53+ return result;
54+ }
55+
56+ int OnSystemInfoInvoked (const unsigned char * payload, size_t size, unsigned char ** response, size_t * response_size)
57+ {
58+ String output;
59+
60+ DynamicJsonDocument doc (1024 );
61+
62+ doc[" chipRevision" ] = ESP .getChipRevision ();
63+ doc[" cpuFreqMHz" ] = ESP .getCpuFreqMHz ();
64+ doc[" cycleCount" ] = ESP .getCycleCount ();
65+ doc[" sdkVersion" ] = ESP .getSdkVersion ();
66+
67+ doc[" flashChip" ][" size" ] = ESP .getFlashChipSize ();
68+ doc[" flashChip" ][" speed" ] = ESP .getFlashChipSpeed ();
69+
70+ doc[" heap" ][" size" ] = ESP .getHeapSize (); // total heap size
71+ doc[" heap" ][" free" ] = ESP .getFreeHeap (); // available heap
72+ doc[" heap" ][" minFree" ] = ESP .getMinFreeHeap (); // lowest level of free heap since boot
73+ doc[" heap" ][" maxAlloc" ] = ESP .getMaxAllocHeap (); // largest block of heap that can be allocated at once
74+
75+ doc[" spi" ][" size" ] = ESP .getPsramSize ();
76+ doc[" spi" ][" free" ] = ESP .getFreePsram ();
77+ doc[" spi" ][" minFree" ] = ESP .getMinFreePsram ();
78+ doc[" spi" ][" maxAlloc" ] = ESP .getMaxAllocPsram ();
79+
80+ doc[" sketch" ][" MD5" ] = ESP .getSketchMD5 ();
81+ doc[" sketch" ][" size" ] = ESP .getSketchSize ();
82+ doc[" sketch" ][" space" ] = ESP .getFreeSketchSpace ();
83+
84+ doc[" fs" ][" usedBytes" ] = SPIFFS .usedBytes ();
85+ doc[" fs" ][" totalBytes" ] = SPIFFS .totalBytes ();
86+
87+ serializeJsonPretty (doc, output);
88+
89+ const char *deviceMethodResponse = output.c_str ();
90+ *response_size = strlen (deviceMethodResponse);
91+ *response = (unsigned char *)malloc (*response_size);
92+ (void )memcpy (*response, deviceMethodResponse, *response_size);
93+
94+ return 200 ;
95+ }
96+
97+ void IoTDevice_RegisterDeviceMethodCallback (){
98+ (void )IoTHubClient_LL_SetDeviceMethodCallback (__hub_client_handle__, IoTDevice_DeviceMethodCallback, NULL );
99+
100+ IoTDevice_RegisterForMethodInvoke (" systemInfo" , OnSystemInfoInvoked);
101+ }
102+
103+ void IoTDevice_RegisterForMethodInvoke (const char *methodName, IOT_DEVICE_METHOD_CALLBACK callback) {
104+ Log_Debug (" Registering method %s at index: %d" , methodName, registeredCount);
105+
106+ if (registeredCount >= IOT_DEVICE_MAX_METHOD_COUNT )
107+ {
108+ Log_Error (" Cannot regiter more than %d methods. Increase the IOT_DEVICE_MAX_METHOD_COUNT variable." , IOT_DEVICE_MAX_METHOD_COUNT );
109+ return ;
110+ }
111+
112+ MethodCallback item = {
113+ .methodName = methodName,
114+ .callback = callback
115+ };
116+
117+ methodCallbacks[registeredCount] = item;
118+ registeredCount++;
119+ }
0 commit comments