Send file data through D7A Action Protocol demo.
Dependencies: modem_ref_helper
main.cpp
- Committer:
- Jeej
- Date:
- 2017-05-18
- Revision:
- 5:4da1ea72e7b4
- Parent:
- 4:45576db9f66c
- Child:
- 6:d4512d8f5dff
File content as of revision 5:4da1ea72e7b4:
// @autor: jeremie@wizzilab.com // @date: 2017-05-02 #include "modem_ref_helper.h" #include "modem_callbacks.h" #include "files.h" #include "sensor.h" Semaphore modem_ready(0); sensor_config_t g_light_config; Queue<void, 8> g_file_modified; uint8_t g_main_id; // Check parameters to see if data should be send static bool report_needed(sensor_config_t* cfg, int32_t value, int32_t last_value, uint32_t last_report_time) { switch (cfg->report_type) { case REPORT_ALWAYS: // Send a report at each measure IPRINT("Report always\r\n"); return true; case REPORT_ON_DIFFERENCE: // Send a report when the difference between the last reported measure and the current mesure is greater than max_diff if (abs(last_value - value) >= cfg->max_diff && cfg->max_diff) { IPRINT("Report on difference (last:%d new:%d max_diff:%d)\r\n", last_value, value, cfg->max_diff); return true; } break; case REPORT_ON_THRESHOLD: // Send a report when crossing a threshold if ( (value >= cfg->threshold_high && last_value < cfg->threshold_high) || (value <= cfg->threshold_low && last_value > cfg->threshold_low) || (value < cfg->threshold_high && last_value >= cfg->threshold_high) || (value > cfg->threshold_low && last_value <= cfg->threshold_low)) { IPRINT("Rerport on threshold (last:%d new:%d th:%d tl:%d)\r\n", last_value, value, cfg->threshold_high, cfg->threshold_low); return true; } break; default: break; } // Send a report if it's been more than max_period since the last report if (((last_report_time/1000) >= cfg->max_period) && cfg->max_period) { IPRINT("Report on period (max_period:%d time:%d)\r\n", cfg->max_period, last_report_time); return true; } return false; } void thread_sensor_light() { uint8_t light_level; uint8_t light_level_old = 0; // To force a first report uint32_t last_report_time = 0xFFFFFFFF; FPRINT("(id:0x%08x)\r\n", osThreadGetId()); // Get the sensor configuration ram_fs_read(FID_SENSOR_CONFIG, 0, SIZE_SENSOR_CONFIG, (uint8_t*)&g_light_config); while (true) { light_level = sensor_get_light(); PRINT("Light %d\r\n", light_level); if (report_needed(&g_light_config, light_level, light_level_old, last_report_time)) { PRINT("Light report %d\r\n", light_level); // Send notification modem_write_file(FID_SENSOR_LIGHT, &light_level, 0, SIZE_SENSOR_LIGHT, g_main_id); modem_ready.wait(); // Update light_level_old = light_level; last_report_time = 0; } // Update last report time last_report_time += g_light_config.read_period; Thread::wait(g_light_config.read_period); } } void thread_file_modified() { uint8_t fid; osEvent evt; while (true) { evt = g_file_modified.get(); fid = (evt.status == osEventMessage)? (uint8_t)(uint32_t)evt.value.p : NULL; switch (fid) { case FID_SENSOR_CONFIG: // Update sensor configuration ram_fs_read(FID_SENSOR_CONFIG, 0, SIZE_SENSOR_CONFIG, (uint8_t*)&g_light_config); PRINT("Sensor configuration updated\r\n"); break; default: break; } } } // Misc // ============================================================{{{ void my_get_alp_file_props(uint8_t fid, alp_file_header_t* hdr) { memcpy(hdr, ram_fs_get_header(fid), sizeof(alp_file_header_t)); } void print_status(int status) { switch (status) { case ALP_ERR_NONE: PRINT("Status: OK\n"); break; case ALP_ERR_FILE_EXIST: PRINT("Status: Already registered\n"); break; default: PRINT("Status: error %d\n", status); break; } } void print_resp(int status) { switch (status) { case ALP_ERR_NONE: PRINT("Resp: OK\n"); break; case ALP_ERR_FILE_EXIST: PRINT("Resp: Already registered\n"); break; default: PRINT("Resp: error %d\n", status); break; } } modem_callbacks_t callbacks = { .read = my_read, .write = my_write, .read_fprop = my_read_fprop, .flush = my_flush, .remove = my_delete, .udata = my_udata, .lqual = my_lqual, .ldown = my_ldown, .reset = my_reset, .boot = my_boot }; // Callback for g_main_id User void my_main_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)id; if (terminal) { print_status(err); modem_ready.release(); } else { print_resp(err); } } /*** Main function ------------------------------------------------------------- ***/ int main() { // Start & initialize #ifdef DEBUG_LED DBG_OPEN(DEBUG_LED); #else DBG_OPEN(NC); #endif PRINT("\r\n--- Starting new run ---\r\n"); FPRINT("(id:0x%08x)\r\n", osThreadGetId()); modem_helper_open(&callbacks); g_main_id = modem_get_id(my_main_callback); DPRINT("Register Files\n"); // HOST Revision is a local file. Uses D7AActP Notification. modem_update_file(FID_HOST_REV, (alp_file_header_t*)&h_rev, (uint8_t*)&f_rev); // Create report file on modem. modem_update_file(FID_SENSOR_LIGHT, (alp_file_header_t*)&h_sensor_light, NULL); // Allow remote access. modem_update_file(FID_SENSOR_CONFIG, (alp_file_header_t*)&h_sensor_config, (uint8_t*)&f_sensor_config); // Configure URC: LQUAL on report file notification every 10 reports PRINT("Setup URCs\n"); modem_enable_urc(ALP_URC_TYPE_LQUAL, IFID_REPORT, 10, true, g_main_id); modem_ready.wait(); PRINT("Start D7A Stack\n"); modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP , true, g_main_id); modem_ready.wait(); PRINT("Notify Modem Version\n"); modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, g_main_id); modem_ready.wait(); PRINT("Notify FW Version\n"); modem_notify_file(FID_HOST_REV, 0, SIZE_HOST_REV, g_main_id); modem_ready.wait(); // Start file modified thread Thread th_file_modified(osPriorityNormal, 1024, NULL); osStatus status = th_file_modified.start(thread_file_modified); ASSERT(status == osOK, "Failed to start thread_file_modified (err: %d)\r\n", status); // Start light measure thread Thread th_sensor_light(osPriorityNormal, 1024, NULL); status = th_sensor_light.start(thread_sensor_light); ASSERT(status == osOK, "Failed to start thread_sensor_light (err: %d)\r\n", status); #ifdef DEBUG_LED DigitalOut my_led(DEBUG_LED); #endif // Set main task to lowest priority osThreadSetPriority(osThreadGetId(), osPriorityIdle); while(true) { Thread::wait(500); #ifdef DEBUG_LED my_led = !my_led; #endif } }