ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers device_controller.cpp Source File

device_controller.cpp

00001 // Copyright (2016) Baidu Inc. All rights reserved.
00002 /**
00003  * File: device_controller.cpp
00004  * Desc: Demo for control device.
00005  */
00006 #include "device_controller.h"
00007 #include "baidu_ca_scheduler.h"
00008 #include "baidu_media_manager.h"
00009 #include "duer_log.h"
00010 
00011 namespace duer {
00012 
00013 #if defined(TEST_BOARD)
00014 
00015 static bca_status_t media_stop(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) {
00016     bca_handler handler = (bca_handler)ctx;
00017     DUER_LOGV("media_stop");
00018 
00019     if (handler && msg) {
00020         duer::MediaManager::instance().stop();
00021         duer::Scheduler::instance().response(msg, BCA_MSG_RSP_CHANGED, NULL);
00022     }
00023 
00024     return BCA_NO_ERR;
00025 }
00026 
00027 static bca_status_t set_volume(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) {
00028     bca_handler handler = (bca_handler)ctx;
00029     static int volume = 10;
00030     int msg_code = BCA_MSG_RSP_CHANGED;
00031     const int LEN = 3;
00032     char str_volume[LEN] = {0};
00033     DUER_LOGV("set_volume");
00034 
00035     if (handler && msg) {
00036         if (msg->msg_code == BCA_MSG_REQ_GET) {
00037             DUER_LOGI("volume get: %d", volume);
00038             snprintf(str_volume, LEN, "%d", volume);
00039         } else if (msg->msg_code == BCA_MSG_REQ_PUT) {
00040             if (msg->payload && msg->payload_len > 0 && msg->payload_len < LEN) {
00041                 snprintf(str_volume, LEN, "%s", (char*)msg->payload);
00042                 DUER_LOGI("volume set: %s", str_volume);
00043                 int vol = atoi(str_volume);
00044 
00045                 if (vol > 16 || vol < 0) {
00046                     msg_code = BCA_MSG_RSP_FORBIDDEN;
00047                 } else {
00048                     if (volume == vol) {
00049                         msg_code = BCA_MSG_RSP_VALID;
00050                     } else {
00051                         duer::MediaManager::instance().set_volume(vol);
00052                         volume = vol;
00053                     }
00054                 }
00055             } else {
00056                 msg_code = BCA_MSG_RSP_FORBIDDEN;
00057                 str_volume[0] = 0;
00058                 DUER_LOGI("volume set invalid");
00059             }
00060         }
00061 
00062         Scheduler::instance().response(msg, msg_code, str_volume);
00063     }
00064 
00065     return BCA_NO_ERR;
00066 }
00067 
00068 static bca_status_t shutdown(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) {
00069     bca_handler handler = (bca_handler)ctx;
00070     DUER_LOGV("shutdown");
00071 
00072     if (handler && msg) {
00073         duer::Scheduler::instance().response(msg, BCA_MSG_RSP_CHANGED, NULL);
00074         duer::Scheduler::instance().stop();
00075     }
00076 
00077     return BCA_NO_ERR;
00078 }
00079 
00080 static bca_status_t set_mode(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) {
00081     bca_handler handler = (bca_handler)ctx;
00082     static const int LEN = 10;
00083     static char mode[LEN] = {0};
00084     int msg_code = BCA_MSG_RSP_CHANGED;
00085     DUER_LOGV("set_mode");
00086 
00087     if (handler && msg) {
00088         if (msg->msg_code == BCA_MSG_REQ_GET) {
00089             DUER_LOGI("mode get: %s", mode);
00090         } else if (msg->msg_code == BCA_MSG_REQ_PUT) {
00091             if (msg->payload && msg->payload_len > 0) {
00092                 snprintf(mode, LEN, "%s", (char*)msg->payload);
00093                 DUER_LOGI("mode set: %s", mode);
00094             } else {
00095                 msg_code = BCA_MSG_RSP_FORBIDDEN;
00096                 mode[0] = 0;
00097                 DUER_LOGI("mode set invalid");
00098             }
00099         }
00100 
00101         Scheduler::instance().response(msg, msg_code, mode);
00102     }
00103 
00104     return BCA_NO_ERR;
00105 }
00106 
00107 static bca_status_t get_power(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) {
00108     bca_handler handler = (bca_handler)ctx;
00109     static double power = 1.0;
00110     DUER_LOGV("get_power");
00111 
00112     if (handler && msg) {
00113         char str_power[4];
00114         snprintf(str_power, 4, "%lf", power);
00115         DUER_LOGI("power: %s", str_power);
00116         Scheduler::instance().response(msg, BCA_MSG_RSP_CHANGED, str_power);
00117 
00118         if (power > 0.5) {
00119             power -= 0.01;
00120         }
00121     }
00122 
00123     return BCA_NO_ERR;
00124 }
00125 
00126 #endif
00127 
00128 void device_controller_init(void) {
00129 #if defined(TEST_BOARD)
00130     bca_res_t res[] = {
00131         {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_PUT, "stop", media_stop},
00132         {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_PUT | BCA_RES_OP_GET, "volume", set_volume},
00133         {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_PUT, "shutdown", shutdown},
00134         {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_PUT | BCA_RES_OP_GET, "mode", set_mode},
00135         {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_GET, "power", get_power},
00136     };
00137     Scheduler::instance().add_controll_points(res, sizeof(res) / sizeof(res[0]));
00138 #endif
00139 }
00140 
00141 } // namespace duer