Arun Raj / Mbed OS MAXREFDES101_SOURCE

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DSInterface.cpp Source File

DSInterface.cpp

00001 /***************************************************************************
00002 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 ****************************************************************************
00032 */
00033 
00034 #include <ctype.h>
00035 #include <string.h>
00036 #include "DSInterface.h"
00037 
00038 #include "../../Utilities/mxm_assert.h"
00039 #include "../version.h"
00040 #include "Peripherals.h"
00041 #include "utils.h"
00042 #include "Thread.h"
00043 #include "BLE_ICARUS.h"
00044 
00045 
00046 
00047 #define HR_ENABLED     1 << 0
00048 #define SPO2_ENABLED   1 << 1
00049 #define BPT_ENABLED    1 << 2
00050 #define WHRM_ENABLED   1 << 3
00051 #define WSPO2_ENABLED  1 << 4
00052 #define AEC_ENABLED    1 << 5
00053 #define SCD_ENABLED    1 << 6
00054 #define AGC_ENABLED    1 << 7
00055 
00056 
00057 
00058 typedef struct {
00059         uint8_t  fwVersion[3];
00060         uint16_t enAlgoDescBitField;
00061         char me11ProductID;
00062 }fw_type_t;
00063 
00064 const static fw_type_t HUB_FW_TABLE[] = {
00065 
00066         {{1,8,3},   HR_ENABLED   | SPO2_ENABLED  | AGC_ENABLED                             , 'A' },
00067         {{20,1,1},  WHRM_ENABLED | SCD_ENABLED   | AEC_ENABLED                             , 'B' },
00068         {{30,1,1},  WHRM_ENABLED | WSPO2_ENABLED | AGC_ENABLED | SCD_ENABLED | AEC_ENABLED , 'C' },
00069         {{40,0,4},  BPT_ENABLED  | SPO2_ENABLED  | AGC_ENABLED                             , 'D' }
00070 
00071 };
00072 
00073 static const char* ActivAlgoInfo[32] = {
00074         [0]   = "hr,",
00075         [1]   = "spo2,",
00076         [2]   = "bpt,",
00077         [3]   = "whrm,",
00078         [4]   = "wspo2,",
00079         [5]   = "aec,",
00080         [6]   = "scd,",
00081         [7]   = "agc,",
00082 };
00083 
00084 
00085 static const char *glbl_cmds[] = {
00086     "stop",
00087     "get_device_info",
00088     "silent_mode 0",
00089     "silent_mode 1",
00090     "pause 0",
00091     "pause 1",
00092     "enable console",
00093     "disable console",
00094     "set_cfg lcd time",
00095     "set_cfg flash log",
00096     "set_cfg stream ascii", // set streaming mode to ascii
00097     "set_cfg stream bin",   // set streaming mode to binary
00098     "set_cfg report 1",     //set report mode to 1 (brief)
00099     "set_cfg report 2"      // set report mode to 2 (long)
00100 };
00101 
00102 typedef enum {
00103     stop=0,
00104     get_device_info,
00105     silent0_mode,
00106     silent1_mode,
00107     pause0_mode,
00108     pause1_mode,
00109     enable_console,
00110     disable_console,
00111     set_lcd_time,
00112     set_flash_log,
00113     enable_ascii_mode,
00114     enable_bin_mode,
00115     set_cfg_report_1,
00116     set_cfg_report_2,
00117     NUM_CMDS,
00118 } glbl_cmd_state;
00119 
00120 
00121 DSInterface::DSInterface(USBSerial* USB)
00122     :flash_thread_(osPriorityNormal, 2 * 1024)
00123 {
00124     cmd_idx = 0;
00125     silent_mode = false;
00126     pause_mode = false;
00127 
00128     memset(sensor_list, 0, DS_MAX_NUM_SENSORCOMMS * sizeof(SensorComm*));
00129     num_sensors = 0;
00130 
00131     m_USB = USB;
00132 
00133     ds_console_interface_exists_ = false;
00134     recordingStarted = false;
00135 
00136     ds_queue_flash_ = &ds_queue_one_;
00137     ds_queue_fill_  = &ds_queue_two_;
00138     queue_init(ds_queue_flash_, buffer_one_, FLASH_MESSAGE_SIZE, sizeof(buffer_one_));
00139     queue_init(ds_queue_fill_, buffer_two_, FLASH_MESSAGE_SIZE, sizeof(buffer_two_));
00140     flash_thread_.start(callback(&event_queue_, &EventQueue::dispatch_forever));
00141 }
00142 
00143 DSInterface::~DSInterface()
00144 {
00145 }
00146 
00147 void DSInterface::add_sensor_comm(SensorComm *s)
00148 {
00149     mxm_assert_msg(num_sensors < DS_MAX_NUM_SENSORCOMMS, "Too many sensors added to DSInterface. Increase DS_MAX_NUM_SENSORCOMMS.");
00150     sensor_list[num_sensors++] = s;
00151 }
00152 
00153 void DSInterface::enable_console_interface()
00154 {
00155     ds_console_interface_exists_ = true;
00156 }
00157 
00158 void DSInterface::ds_set_ble_status(bool en){
00159     SensorComm *p_sensor;
00160     ds_ble_interface_exists_ = en;
00161 
00162     for(int sensor_num = 0; sensor_num < num_sensors; ++sensor_num) {
00163         p_sensor = sensor_list[sensor_num];
00164         p_sensor->SensorComm_Set_Ble_Status (ds_ble_interface_exists_);
00165     }
00166 }
00167 
00168 void DSInterface::ds_set_flash_rec_status(bool en) {
00169     SensorComm *p_sensor;
00170     recordingStarted = en;
00171     for(int sensor_num = 0; sensor_num < num_sensors; ++sensor_num) {
00172         p_sensor = sensor_list[sensor_num];
00173         p_sensor->SensorComm_Set_Flash_Status(en);
00174     }
00175 }
00176 
00177 void DSInterface::build_command(char ch)
00178 {
00179     if (!this->silent_mode) /* BUG: POTENTIAL BUG, what uart port to echo, not only console */
00180         m_USB->printf("%c", ch);
00181 
00182     if (ch == 0x00) {
00183         pr_err("Ignored char 0x00");
00184         return;
00185     }
00186 
00187     if ((ch == '\n') || (ch == '\r')) {
00188         if (cmd_idx < (int)CONSOLE_STR_BUF_SZ)
00189         cmd_str[cmd_idx++] = '\0';
00190         m_USB->printf("\r\n");
00191         parse_command();
00192 
00193         //Clear cmd_str
00194         while (cmd_idx > 0) /* BUG: POTENTIAL BUG for multiple port access */
00195             cmd_str[--cmd_idx] = '\0';
00196 
00197     } else if ((ch == 0x08 || ch == 0x7F) && cmd_idx > 0) {
00198         //Backspace character
00199         if (cmd_idx > 0)
00200             cmd_str[--cmd_idx] = '\0';
00201     } else {
00202         /* BUG: POTENTIAL BUG for multiple port access */
00203         if (cmd_idx < (int)CONSOLE_STR_BUF_SZ)
00204             cmd_str[cmd_idx++] = ch;
00205     }
00206 
00207 }
00208 
00209 void DSInterface::stopcommand() {
00210     for (int i = 0; i < num_sensors; i++) {
00211         sensor_list[i]->stop();
00212     }
00213     ds_set_flash_rec_status(false);
00214 
00215 }
00216 
00217 void DSInterface::parse_command_str(const char* cmd ) {
00218     strncpy(cmd_str, cmd , strlen(cmd) + 1);
00219     parse_command();
00220 }
00221 
00222 void DSInterface::write_to_flash_thread_funct() {
00223     //static Timer mytimer;
00224     //mytimer.reset();
00225     //mytimer.start();
00226     fwrite((uint8_t*)ds_queue_flash_->base,  1, ds_queue_flash_->item_size * ds_queue_flash_->num_item,  flashFileHandle);
00227     queue_reset(ds_queue_flash_);
00228     //mytimer.stop();
00229     //printf("%f seconds\n", mytimer.read());
00230 }
00231 
00232 void DSInterface::parse_command()
00233 {
00234     int i;
00235     glbl_cmd_state cmd;
00236     char charbuf[512];
00237     char tempbuf[32];
00238     int data_len = 0;
00239     int data_len_log=0;
00240     int ret;
00241     bool parsed_cmd = true;
00242 
00243     printf("%s \n",cmd_str );
00244 
00245     //If top level command, then handle it
00246     for (i = 0; i < NUM_CMDS; i++) {
00247         if (starts_with(&cmd_str[0], glbl_cmds[i])) {
00248             cmd = (glbl_cmd_state)i;
00249 
00250             switch (cmd) {
00251                 case (enable_ascii_mode): {
00252                     sensor_list[0]->AsciiEn = true;
00253                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00254                             "\r\n%s err=0\r\n", cmd_str);
00255                 } break;
00256                 case (enable_bin_mode): {
00257                     sensor_list[0]->AsciiEn = false;
00258                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00259                             "\r\n%s err=0\r\n", cmd_str);
00260                 } break;
00261                 case (stop): {
00262                     for (int i = 0; i < num_sensors; i++) {
00263                         sensor_list[i]->stop();
00264                     }
00265 
00266                     ds_set_flash_rec_status(false);
00267 
00268                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00269                             "\r\n%s err=0\r\n", cmd_str);
00270                 } break;
00271                 case (get_device_info): {
00272 
00273                     data_len = snprintf(charbuf, sizeof(charbuf),
00274                         "\r\n%s platform=%s firmware_ver=%s sensors=", cmd_str, platform_name, FIRMWARE_VERSION);
00275 
00276                     //Add list of sensors
00277                     for (int i = 0; i < num_sensors; i++) {
00278                         if (sensor_list[i]->is_visible()) {
00279                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00280                                             "%s", sensor_list[i]->get_type());
00281                             if (i < (num_sensors - 1))
00282                                 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, ",");
00283                         }
00284                     }
00285 
00286 
00287                     for (int i = 0; i < num_sensors; i++) {
00288                         SensorComm *s = sensor_list[i];
00289                         if (!s->is_visible())
00290                             continue;
00291 
00292                         //Add algo_ver
00293                         data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00294                                             " algo_ver_%s=%s", s->get_type(), s->get_algo_ver());
00295 
00296                         //Add part name
00297                         data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00298                                             " part_name_%s=%s", s->get_type(), s->get_part_name());
00299 
00300                         uint8_t part_id, part_rev;
00301                         ret = s->get_part_info(&part_id, &part_rev);
00302 
00303                         if (ret == 0) {
00304                             //Add part id
00305                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00306                                             " part_id_%s=%d", s->get_type(), part_id);
00307 
00308                             //Add rev id
00309                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00310                                             " part_rev_%s=%d", s->get_type(), part_rev);
00311                         }
00312                     }
00313 
00314                     if(firmware_version){
00315                         data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, " hub_firm_ver=%s", firmware_version);
00316                         if(algo_desc_strsz > 0)
00317                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, "%s", algo_desc_string);
00318                     }
00319 
00320                     data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, " err=0\r\n");
00321 
00322                 } break;
00323                 case (silent0_mode): {
00324                     silent_mode = false;
00325                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00326                             "\r\n%s err=0\r\n", cmd_str);
00327                 } break;
00328                 case (silent1_mode): {
00329                     silent_mode = true;
00330                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00331                             "\r\n%s err=0\r\n", cmd_str);
00332                 } break;
00333                 case (pause0_mode): {
00334                     pause_mode = false;
00335                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00336                             "\r\n%s err=0\r\n", cmd_str);
00337                 } break;
00338                 case (pause1_mode): {
00339                     pause_mode = true;
00340                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00341                             "\r\n%s err=0\r\n", cmd_str);
00342                 } break;
00343                 case (enable_console): {
00344                     ds_console_interface_exists_ = true;
00345                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00346                             "\r\n%s err=0\r\n", cmd_str);
00347                 } break;
00348                 case (disable_console): {
00349                     ds_console_interface_exists_ = false;
00350                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00351                             "\r\n%s err=0\r\n", cmd_str);
00352                 } break;
00353               case (set_lcd_time): {
00354 
00355                 while(1)
00356                 {
00357                     if(cmd_str[17+lcd_time_count] == '\0')
00358                     {
00359                         lcd_time_str[lcd_time_count] = '\0';
00360                         break;
00361                     }
00362                     lcd_time_str[lcd_time_count] = cmd_str[17+lcd_time_count];
00363                     lcd_time_count++;
00364                 }
00365 
00366                 sscanf(lcd_time_str,"%d",&lcd_time_val);
00367 
00368                 set_time(lcd_time_val);
00369 
00370                 lcd_time_count=0;
00371 
00372                 data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00373                         "\r\n%s err=0\r\n", cmd_str);
00374 
00375                 } break;
00376               case (set_flash_log): {
00377 
00378 
00379 
00380                     if(cmd_str[18] == '1' )
00381                     {
00382 
00383 
00384 
00385                         time_t seconds;
00386                         seconds = time(NULL);
00387                         strftime(tempbuf, 32, "/fs/log-%I-%M-%S.maximlog", localtime(&seconds));
00388 
00389                         flashFileHandle = fopen(tempbuf, "w+");
00390 
00391                         if ( flashFileHandle != NULL ) {
00392 
00393                             fprintf(flashFileHandle,"mxim");
00394 
00395                             //1 byte to indicate the version of the log
00396                             unsigned char vers = 0;
00397                             //4 bytes to indicate the number of bytes used to write the format
00398                             unsigned int NumByteBLE = 764;
00399                             unsigned int NumByteNoBLE = 659;
00400 
00401                             fwrite(&vers,sizeof(unsigned char),1,flashFileHandle);
00402 
00403                             if(BLE::Instance().gap().getState().connected){
00404                                 fwrite(&NumByteBLE,sizeof(unsigned int),1,flashFileHandle);
00405                             }
00406                             else
00407                                 fwrite(&NumByteNoBLE,sizeof(unsigned int),1,flashFileHandle);
00408 
00409                             ds_set_flash_rec_status(true);
00410 
00411                             //Write the formats
00412                             data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf) - data_len_log- 1,"ppg 0 cs=1 format={smpleCnt,16},"
00413                                     "{grnCnt,20},{grn2Cnt,20},{accelX,14,3},{accelY,14,3},"
00414                                     "{accelZ,14,3},{hr,12},{hrconf,8},{r,11,1},{activity,8}\n"
00415                                     "ppg 1 cs=1 format={smpleCnt,8},{irCnt,20},{redCnt,20},{accelX,14,3},"
00416                                     "{accelY,14,3},{accelZ,14,3},{r,12,1},{wspo2conf,8},"
00417                                     "{spo2,11,1},{wspo2percentcomplete,8},{wspo2lowSNR,1},{wspo2motion,1},{status,8}\n"
00418                                     "bpt 0 cs=1 format={status,4},{irCnt,19},{hr,9},"
00419                                     "{prog,9},{sys_bp,9},{dia_bp,9}\n"
00420                                     "bpt 1 cs=1 format={status,4},{irCnt,19},{hr,9},"
00421                                     "{prog,9},{sys_bp,9},{dia_bp,9}\n");
00422                             if(BLE::Instance().gap().getState().connected){
00423                                 data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf) -data_len_log- 1,"ecg 1 cs=1 format={smpleCnt,8},{rtor,14},{rtorbpm,8},"
00424                                         "{pTag.0,3},{eTag.0,3},{ecg.0,18},{pTag.1,3},{eTag.1,3},{ecg.1,18},"
00425                                         "{pTag.2,3},{eTag.2,3},{ecg.2,18},{pTag.3,3},{eTag.3,3},{ecg.3,18}\n");
00426                             }else{
00427                                 data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf) -data_len_log- 1,"ecg 1 cs=1 format={smpleCnt,8},{pTag,3},{eTag,3},"
00428                                         "{ecg,18},{rtor,14},{rtorbpm,8}\n");
00429                             }
00430                             data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf)-data_len_log - 1,"ecg 2 cs=1 format={smplCnt,8},{rtor,15}\n");
00431                             data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf) -data_len_log-1,"temp 0 cs=1 format={smpleCnt,8},{temp,16,2}\n");
00432 
00433 
00434                             fprintf(flashFileHandle,logbuf,data_len_log);
00435                         }
00436                         else {
00437                             ds_set_flash_rec_status(false);
00438                         }
00439 
00440                         data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00441                                 "\r\n%s err=0\r\n", cmd_str);
00442 
00443 
00444 
00445                     }
00446 
00447                     else if (cmd_str[18] == '0'){
00448                         ds_set_flash_rec_status(false);
00449                         handle_file_close();
00450                         data_len += snprintf(charbuf, sizeof(charbuf) - 1,"\r\n%s err=0\r\n", cmd_str);
00451                     }
00452                     else{
00453                         //print cmd not recognized string
00454                         data_len += snprintf(charbuf, sizeof(charbuf) - 1,"\r\n%s err=-255\r\n", cmd_str);
00455                     }
00456 
00457                 } break;
00458                 case (set_cfg_report_1): {
00459                     //m_USB->printf("\r\n MODE 1\r\n");
00460                     algo_report_mode = 1;
00461                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00462                             "\r\n%s err=0\r\n", cmd_str);
00463                     
00464                 } break;
00465                 case (set_cfg_report_2): {
00466                     //m_USB->printf("\r\n MODE 2\r\n");
00467                     algo_report_mode = 2;
00468                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00469                             "\r\n%s err=0\r\n", cmd_str);
00470                 } break;
00471                 default:
00472                     parsed_cmd = false;
00473                     break;
00474             }
00475 
00476             if (parsed_cmd) {
00477                 m_USB->printf(charbuf);
00478 
00479                 if (BLE::Instance().gap().getState().connected) {
00480                     BLE_Icarus_AddtoQueue((uint8_t *)charbuf, (int32_t)sizeof(charbuf), data_len);
00481                 }
00482 
00483             }
00484 
00485             return;
00486         }
00487     }
00488 
00489     //Set the stream type to check the data streaming type later
00490     if(starts_with(&cmd_str[0],"read ppg 0")){
00491         stream_type=0x00;
00492     }
00493     else if(starts_with(&cmd_str[0],"read ppg 1")){
00494         stream_type=0x01;
00495     }
00496     else if(starts_with(&cmd_str[0],"read bpt 0")){
00497         stream_type=0x02;
00498     }
00499     else if(starts_with(&cmd_str[0],"read bpt 1")){
00500         stream_type=0x03;
00501     }
00502     else if(starts_with(&cmd_str[0],"read ecg 1")){
00503         stream_type=0x04;
00504     }
00505     else if(starts_with(&cmd_str[0],"read ecg 2")){
00506         stream_type=0x05;
00507     }
00508     else if(starts_with(&cmd_str[0],"read temp 0")){
00509         stream_type=0x06;
00510     }
00511 
00512     //Loop through each sensor in sensorList
00513     //If sensor.get_type() is contained in cmd_str, pass cmd_str to that sensor's parser
00514     for (int i = 0; i < num_sensors; i++) {
00515         if (strstr(&cmd_str[0], sensor_list[i]->get_type())) {
00516             if (sensor_list[i]->parse_command(cmd_str))
00517                 return;
00518 
00519             break;
00520         }
00521     }
00522 
00523     //If we still haven't found a way to parse the command,
00524     //send it to every sensor until one handles it
00525     for (int i = 0; i < num_sensors; i++) {
00526         if (sensor_list[i]->parse_command(cmd_str))
00527             return;
00528     }
00529     
00530 
00531     //No one could handle the command, print cmd not recognized string
00532     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00533             "\r\n%s err=-255\r\n", cmd_str);
00534     m_USB->printf(charbuf);
00535 
00536     if (BLE::Instance().gap().getState().connected) {
00537         BLE_Icarus_AddtoQueue((uint8_t *)charbuf, (int32_t)sizeof(charbuf), data_len);
00538     }
00539 
00540 }
00541 
00542 void  DSInterface::swap_queues() {
00543     if(flash_thread_.get_state() != Thread::WaitingEventFlag){
00544         printf("overflow happened state %d\r\n", flash_thread_.get_state());
00545         return;
00546     }
00547     queue_t * temp_queue = ds_queue_flash_;
00548     ds_queue_flash_ = ds_queue_fill_;
00549     ds_queue_fill_ = temp_queue;
00550     event_queue_.call(Callback<void()>(this, &DSInterface::write_to_flash_thread_funct));
00551 }
00552 
00553 void DSInterface::handle_file_close() {
00554     //if recording is enabled do not close the file
00555     if(recordingStarted) {
00556         return;
00557     }
00558     if(flashFileHandle != NULL) {
00559         if(flash_thread_.get_state() != Thread::WaitingEventFlag)
00560             return;
00561 
00562         if(ds_queue_fill_->num_item){
00563             swap_queues();
00564             return;
00565         }
00566         printf("closing file\r\n");
00567         fclose(flashFileHandle);
00568         flashFileHandle = NULL;
00569     }
00570 }
00571 
00572 
00573 void DSInterface::force_file_close() {
00574     if(flashFileHandle != NULL) {
00575         while(flash_thread_.get_state() != Thread::WaitingEventFlag) {
00576             wait_ms(1);
00577         }
00578 
00579         if(ds_queue_fill_->num_item){
00580             swap_queues();
00581         }
00582 
00583         while(flash_thread_.get_state() != Thread::WaitingEventFlag) {
00584             wait_ms(1);
00585         }
00586 
00587         //printf("closing file\r\n");
00588         fclose(flashFileHandle);
00589         flashFileHandle = NULL;
00590     }
00591 }
00592 
00593 #define ECGSENSOR_ID 0x02                   //TODO:FIX THESE
00594 #define ECGSENSOR_DATA_REPORT_MODE 0x04     //TODO:FIX THESE
00595 void DSInterface::data_report_execute() {
00596     char buffer[256];
00597     int data_len = 0;
00598     buffer[0] = '\0';
00599     int ret;
00600     bool all_sensors_disabled = true;
00601 
00602     for (int i = 0; i < num_sensors; i++) {
00603         //if ((*it)->is_enabled()) {
00604 
00605         if(!recordingStarted)
00606             data_len = sensor_list[i]->data_report_execute(buffer, sizeof(buffer));
00607         else
00608             data_len = sensor_list[i]->data_report_execute(buffer + FLASH_MESSAGE_OFFSET
00609             , sizeof(buffer) - FLASH_MESSAGE_OFFSET);
00610 
00611         if (!this->pause_mode && (data_len > 0)) {
00612             all_sensors_disabled = false;
00613 
00614             if ((flashFileHandle != NULL) && (recordingStarted)) {
00615                 if(data_len + FLASH_MESSAGE_OFFSET != ds_queue_flash_->item_size) {
00616                     printf("Reinit item size: %d\r\n", data_len);
00617                     queue_update_items_size(ds_queue_flash_, (data_len+ FLASH_MESSAGE_OFFSET));
00618                     queue_update_items_size(ds_queue_fill_, (data_len+ FLASH_MESSAGE_OFFSET));
00619                 }
00620                 //set stream type
00621                 buffer[0] = stream_type;
00622 
00623                 // enqueue data
00624                 ret = enqueue(ds_queue_fill_, buffer);
00625                 if(ret != 0) {
00626                     printf("enqueue has failed %d\r\n", ds_queue_fill_->num_item);
00627                 }
00628                 if(queue_is_full(ds_queue_fill_)) {
00629                     swap_queues();
00630                 }
00631             }
00632 
00633 
00634             if (BLE::Instance().gap().getState().connected & !recordingStarted) {
00635                 ds_console_interface_exists_ = false;
00636                 BLE_Icarus_AddtoQueue((uint8_t *)buffer, (int32_t)sizeof(buffer), data_len);
00637             }
00638 
00639             if (ds_console_interface_exists_ & !recordingStarted){
00640                 if(sensor_list[0]->AsciiEn){
00641                     m_USB->printf(buffer);
00642                 }
00643                 else{
00644                     m_USB->writeBlock((uint8_t*)buffer, data_len);
00645                 }
00646             }
00647             data_len = 0;
00648         }
00649     }
00650 
00651     if(all_sensors_disabled)
00652         handle_file_close();
00653 }
00654 
00655 void DSInterface::set_fw_version(const char* version)
00656 {
00657     if (version && *version){
00658         firmware_version = version;
00659         algo_desc_strsz = get_fw_algorithms();
00660     }
00661 }
00662 
00663 int DSInterface::get_fw_algorithms( void )
00664 {
00665 
00666     int i, fwToReport;
00667     char *start, *end;
00668     uint8_t fwIdx[3];
00669 
00670     start = (char*) firmware_version;
00671 
00672     /*MYG: no need to optimize ,lib function call, just called once at start of execution*/
00673 
00674     fwIdx[0] = (uint8_t) strtol(start,&end, 10)  ; start = end;
00675     fwIdx[1] = (uint8_t) strtol(start+1,&end, 10); start = end;
00676     fwIdx[2] = (uint8_t) strtol(start+1,&end, 10);
00677 
00678     fwToReport = -1;
00679     for( i = 0 ; i < sizeof(HUB_FW_TABLE)/ sizeof(fw_type_t); i++ ){
00680 
00681         if( HUB_FW_TABLE[i].fwVersion[0] == fwIdx[0]/* &
00682             HUB_FW_TABLE[i].fwVersion[1] == fwIdx[1] &
00683             HUB_FW_TABLE[i].fwVersion[2] == fwIdx[2] */) {
00684 
00685                fwToReport = i;
00686                break;
00687             }
00688      }
00689 
00690     if(fwToReport == -1){
00691         return -1;
00692     }
00693 
00694      i = ((fwToReport == -1)? 0:1) * ( /*32 bit*/ 32 - __builtin_clz(HUB_FW_TABLE[fwToReport].enAlgoDescBitField));
00695 
00696      strcpy(&algo_desc_string[0], " fw_algos=");
00697      int tail = strlen(" fw_algos=");
00698 
00699      int descStrSz      = sizeof(algo_desc_string);
00700      while(--i && tail < descStrSz) {
00701 
00702          if(  (HUB_FW_TABLE[fwToReport].enAlgoDescBitField >> i) & 0x0001) {
00703                strcpy( &algo_desc_string[tail], ActivAlgoInfo[i]);
00704                tail += strlen(ActivAlgoInfo[i]);
00705          }
00706 
00707      }
00708      strcpy( &algo_desc_string[tail-1]," ");
00709 
00710      return tail;
00711      //m_USB->printf("%d %s \r\n", tail , algo_desc_string);
00712 
00713 }
00714 
00715 void DSInterface::set_fw_platform(const char* platform)
00716 {
00717     if (platform && *platform)
00718         platform_name = platform;
00719 }