Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion GenX_Generated/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"AZURE_SPHERE_TARGET_API_SET": "9"
},
"cmake.configureOnOpen": true,
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"files.associations": {
"gx_bindings.h": "c"
}
}
2 changes: 1 addition & 1 deletion GenX_Generated/AzureSphereDevX
2 changes: 1 addition & 1 deletion GenX_Generated/Drivers
4 changes: 2 additions & 2 deletions GenX_Generated/app_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"AllowedConnections": [
"REPLACE_WITH_REQUIRED_ENDPOINTS"
],
"SystemEventNotifications": false,
"SoftwareUpdateDeferral": false,
"SystemEventNotifications": true,
"SoftwareUpdateDeferral": true,
"PowerControls": []
},
"ApplicationType": "Default",
Expand Down
3 changes: 3 additions & 0 deletions GenX_Generated/gx_includes/gx_DeferredUpdate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#define GX_DEFERRED_UPDATE true
7 changes: 7 additions & 0 deletions GenX_Generated/gx_includes/gx_Watchdog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

/****************************************************************************************
* Watchdog support
****************************************************************************************/
const struct itimerspec watchdogInterval = { { 60, 0 },{ 60, 0 } };
timer_t watchdogTimer;
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include "hw/azure_sphere_learning_path.h" // Hardware definition

#include "dx_azure_iot.h"
#include "dx_config.h"
#include "dx_exit_codes.h"
Expand All @@ -12,42 +10,38 @@
#include "dx_version.h"
#include "dx_deferred_update.h"

#include "gx_includes.h"

#include <time.h>

DX_USER_CONFIG dx_config; // cmd args from app_manifest.json

extern const char PNP_MODEL_ID[];
extern const char NETWORK_INTERFACE[];

#define ONE_MS 1000000 // used to simplify timer defn.
#define Log_Debug(f_, ...) dx_Log_Debug((f_), ##__VA_ARGS__)
static char Log_Debug_buffer[128];


/****************************************************************************************
* Forward declarations
****************************************************************************************/
static void PressureAlertLevel_gx_handler(DX_DEVICE_TWIN_BINDING* deviceTwinBinding);
static DX_DECLARE_DIRECTMETHOD_HANDLER(FanOn_gx_handler);
static DX_DECLARE_DIRECTMETHOD_HANDLER(OfficeLightOn_gx_handler);
static DX_DECLARE_TIMER_HANDLER(MeasureTemperature_gx_handler);


/****************************************************************************************
* Binding declarations
****************************************************************************************/
static DX_DEVICE_TWIN_BINDING dt_PressureAlertLevel = { .twinProperty = "PressureAlertLevel", .twinType = DX_TYPE_FLOAT, .handler = PressureAlertLevel_gx_handler };
static DX_GPIO_BINDING gpio_Light = { .pin = NETWORK_CONNECTED_LED, .name = "Light", .direction = DX_OUTPUT, .initialState = GPIO_Value_Low, .invertPin = false };
static DX_DIRECT_METHOD_BINDING dm_OfficeLightOn = { .methodName = "OfficeLightOn", .handler = OfficeLightOn_gx_handler, .context=&gpio_Light };
static DX_DIRECT_METHOD_BINDING dm_FanOn = { .methodName = "FanOn", .handler = FanOn_gx_handler, .context=&gpio_Fan };
static DX_TIMER_BINDING tmr_MeasureTemperature = { .period = { 5, 0 }, .name = "MeasureTemperature", .handler = MeasureTemperature_gx_handler };


// All direct methods referenced in direct_method_bindings will be subscribed to in the gx_initPeripheralAndHandlers function
static DX_DEVICE_TWIN_BINDING* device_twin_bindings[] = { };
static DX_DEVICE_TWIN_BINDING* device_twin_binding_set[] = { &dt_PressureAlertLevel };

// All direct methods referenced in direct_method_bindings will be subscribed to in the gx_initPeripheralAndHandlers function
static DX_DIRECT_METHOD_BINDING *direct_method_bindings[] = { };
static DX_DIRECT_METHOD_BINDING *direct_method_binding_set[] = { &dm_OfficeLightOn, &dm_FanOn };

// All GPIOs referenced in gpio_bindings with be opened in the gx_initPeripheralAndHandlers function
static DX_GPIO_BINDING *gpio_bindings[] = { };
static DX_GPIO_BINDING *gpio_binding_set[] = { &gpio_Light };

// All timers referenced in timer_bindings will be opened in the gx_initPeripheralAndHandlers function
#define DECLARE_DX_TIMER_BINDINGS
static DX_TIMER_BINDING *timer_bindings[] = { };
static DX_TIMER_BINDING *timer_binding_set[] = { &tmr_MeasureTemperature };

// All timers referenced in timer_bindings_oneshot will be started in the gx_initPeripheralAndHandlers function
static DX_TIMER_BINDING *timer_bindings_oneshot[] = { };
Expand All @@ -63,25 +57,25 @@ static void gx_initPeripheralAndHandlers(void)
#ifdef GX_AZURE_IOT
dx_azureConnect(&dx_config, NETWORK_INTERFACE, PNP_MODEL_ID);
#else
if (NELEMS(device_twin_bindings) > 0 || NELEMS(direct_method_bindings) > 0) {
if (NELEMS(device_twin_binding_set) > 0 || NELEMS(direct_method_binding_set) > 0) {
dx_azureConnect(&dx_config, NETWORK_INTERFACE, PNP_MODEL_ID);
}
#endif // GX_AZURE_IOT

if (NELEMS(gpio_bindings) > 0) {
dx_gpioSetOpen(gpio_bindings, NELEMS(gpio_bindings));
if (NELEMS(gpio_binding_set) > 0) {
dx_gpioSetOpen(gpio_binding_set, NELEMS(gpio_binding_set));
}

if (NELEMS(device_twin_bindings) > 0) {
dx_deviceTwinSubscribe(device_twin_bindings, NELEMS(device_twin_bindings));
if (NELEMS(device_twin_binding_set) > 0) {
dx_deviceTwinSubscribe(device_twin_binding_set, NELEMS(device_twin_binding_set));
}

if (NELEMS(direct_method_bindings) > 0 ) {
dx_directMethodSubscribe(direct_method_bindings, NELEMS(direct_method_bindings));
if (NELEMS(direct_method_binding_set) > 0 ) {
dx_directMethodSubscribe(direct_method_binding_set, NELEMS(direct_method_binding_set));
}

if (NELEMS(timer_bindings) > 0) {
dx_timerSetStart(timer_bindings, NELEMS(timer_bindings));
if (NELEMS(timer_binding_set) > 0) {
dx_timerSetStart(timer_binding_set, NELEMS(timer_binding_set));
}

if (NELEMS(timer_bindings_oneshot) > 0) {
Expand All @@ -98,12 +92,12 @@ static void gx_initPeripheralAndHandlers(void)
}

static void gx_closePeripheralAndHandlers(void){
if (NELEMS(timer_bindings) > 0) {
dx_timerSetStop(timer_bindings, NELEMS(timer_bindings));
if (NELEMS(timer_binding_set) > 0) {
dx_timerSetStop(timer_binding_set, NELEMS(timer_binding_set));
}

if (NELEMS(gpio_bindings) > 0) {
dx_gpioSetClose(gpio_bindings, NELEMS(gpio_bindings));
if (NELEMS(gpio_binding_set) > 0) {
dx_gpioSetClose(gpio_binding_set, NELEMS(gpio_binding_set));
}

dx_deviceTwinUnsubscribe();
Expand Down
131 changes: 100 additions & 31 deletions GenX_Generated/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
************************************************************************************************/

#include "gx_includes/gx_declarations.h"
#include "main.h"

#include "app_exit_codes.h"

Expand All @@ -47,51 +47,120 @@
const char PNP_MODEL_ID[] = "dtmi:com:example:application;1";
const char NETWORK_INTERFACE[] = "wlan0";



/// <summary>
/// Initialize gpios, device twins, direct methods, timers.
/// </summary>
static void InitPeripheralAndHandlers(void) {{
dx_Log_Debug_Init(Log_Debug_buffer, sizeof(Log_Debug_buffer));
gx_initPeripheralAndHandlers();
static void InitPeripheralAndHandlers(void)
{
{
dx_Log_Debug_Init(Log_Debug_buffer, sizeof(Log_Debug_buffer));
gx_initPeripheralAndHandlers();

// Uncomment the StartWatchdog when ready for production
// StartWatchdog();
}}
// Uncomment the StartWatchdog when ready for production
// StartWatchdog();
}
}

/// <summary>
/// Close Timers, GPIOs, Device Twins, Direct Methods
/// </summary>
static void ClosePeripheralAndHandlers(void) {{
dx_azureToDeviceStop();
gx_closePeripheralAndHandlers();
dx_timerEventLoopStop();
}}
static void ClosePeripheralAndHandlers(void)
{
{
dx_azureToDeviceStop();
gx_closePeripheralAndHandlers();
dx_timerEventLoopStop();
}
}

/// <summary>
/// Main event loop for the app
/// </summary>
int main(int argc, char* argv[]) {{
dx_registerTerminationHandler();
if (!dx_configParseCmdLineArguments(argc, argv, &dx_config)) {{
return dx_getTerminationExitCode();
}}
int main(int argc, char *argv[])
{
{
dx_registerTerminationHandler();
if (!dx_configParseCmdLineArguments(argc, argv, &dx_config))
{
return dx_getTerminationExitCode();
}

InitPeripheralAndHandlers();
InitPeripheralAndHandlers();

// Main loop
while (!dx_isTerminationRequired()) {{
int result = EventLoop_Run(dx_timerGetEventLoop(), -1, true);
// Continue if interrupted by signal, e.g. due to breakpoint being set.
if (result == -1 && errno != EINTR) {{
dx_terminate(DX_ExitCode_Main_EventLoopFail);
}}
}}
// Run the main event loop. This is a blocking call until termination requested
dx_eventLoopRun();

ClosePeripheralAndHandlers();
return dx_getTerminationExitCode();
}}
ClosePeripheralAndHandlers();
return dx_getTerminationExitCode();
}
}

// Main code blocks


/// GENX_BEGIN ID:PressureAlertLevel MD5:9064ac54b264343134d043250a39f0ae
/// <summary>
/// What is the purpose of this device twin handler function?
/// </summary>
/// <param name="deviceTwinBinding"></param>
static DX_DEFINE_DEVICETWIN_HANDLER(PressureAlertLevel_gx_handler, deviceTwinBinding)
{
Log_Debug("Device Twin Property Name: %s\n", deviceTwinBinding->propertyName);
void *value = deviceTwinBinding->propertyValue;
// Test device twin float is within range
if (!IN_RANGE(*(float*)value, 800, 1100))
{
dx_deviceTwinAckDesiredState(deviceTwinBinding, deviceTwinBinding->propertyValue, DX_DEVICE_TWIN_ERROR);
}

// implement your device twin logic

dx_deviceTwinAckDesiredState(deviceTwinBinding, deviceTwinBinding->propertyValue, DX_DEVICE_TWIN_RESPONSE_COMPLETED);

}
DX_END_DEVICETWIN_HANDLER
/// GENX_END ID:PressureAlertLevel


/// GENX_BEGIN ID:OfficeLightOn MD5:20183fbd448dd3c7a6095c0653aa1d8e
/// <summary>
/// OfficeLightOn direct method purpose
/// </summary>
static DX_DEFINE_DIRECTMETHOD_HANDLER(OfficeLightOn_gx_handler, json, directMethodBinding, responseMsg)
{
DX_GPIO_BINDING context = (DX_GPIO_BINDING *)directMethodBinding->context;
dx_gpioOn(context);
return DX_METHOD_SUCCEEDED;
}
DX_END_DIRECTMETHOD_HANDLER
/// GENX_END ID:OfficeLightOn


/// GENX_BEGIN ID:FanOn MD5:c889b5c2ef38a0ee5b8a75ee84fdb7d5
/// <summary>
/// FanOn direct method purpose
/// </summary>
static DX_DEFINE_DIRECTMETHOD_HANDLER(FanOn_gx_handler, json, directMethodBinding, responseMsg)
{
DX_GPIO_BINDING context = (DX_GPIO_BINDING *)directMethodBinding->context;
dx_gpioOn(context);
return DX_METHOD_SUCCEEDED;
}
DX_END_DIRECTMETHOD_HANDLER
/// GENX_END ID:FanOn


/// GENX_BEGIN ID:MeasureTemperature MD5:a822377b049818a5f19b50f26a48884c
/// <summary>
/// MeasureTemperature_gx_handler purpose?
/// </summary>
static DX_DEFINE_TIMER_HANDLER(MeasureTemperature_gx_handler)
{
Log_Debug("Periodic timer MeasureTemperature_handler called\n");

// Implement your timer function

}
DX_END_TIMER_HANDLER
/// GENX_END ID:MeasureTemperature

27 changes: 27 additions & 0 deletions GenX_Generated/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "hw/azure_sphere_learning_path.h" // Hardware definition

#include "dx_azure_iot.h"
#include "dx_config.h"
#include "dx_exit_codes.h"
#include "dx_gpio.h"
#include "dx_json_serializer.h"
#include "dx_terminate.h"
#include "dx_timer.h"
#include "dx_version.h"
#include "dx_deferred_update.h"

#include "gx_includes.h"
#include "gx_bindings.h"

#include <time.h>

DX_USER_CONFIG dx_config; // cmd args from app_manifest.json

extern const char PNP_MODEL_ID[];
extern const char NETWORK_INTERFACE[];

#define Log_Debug(f_, ...) dx_Log_Debug((f_), ##__VA_ARGS__)
static char Log_Debug_buffer[128];

2 changes: 2 additions & 0 deletions Generator/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
/build/
/builders/__pycache__/

*.pyc

19 changes: 10 additions & 9 deletions Generator/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"files.associations": {
"*.template": "c"
},
"json.schemas": [
{
"fileMatch": [
"app_model.json"
],
"url": "./app_model.schema.json"
}
]
}
// ,
// "json.schemas": [
// {
// "fileMatch": [
// "app_model.json"
// ],
// "url": "./app_model.schema.json"
// }
// ]
}
Loading