Exemple #1
0
int wifi_send_ip(void){
  int TX_BUF_SIZE = LG_BUF_SIZE;
  int PAYLOAD_BUF_SIZE = LG_BUF_SIZE;
  int r;
  char *tx_buf,*payload_buf;
  tx_buf = core_malloc(TX_BUF_SIZE);
  payload_buf = core_malloc(PAYLOAD_BUF_SIZE);
  snprintf(payload_buf,PAYLOAD_BUF_SIZE,
	   "{\"serial_number\":\"%s\",\"ip_addr\":\"%s\"}",
	   wemo_config.serial_number,wemo_config.ip_addr);
  snprintf(tx_buf,TX_BUF_SIZE,
	   "POST /config/plugs/update HTTP/1.1\r\n"
	   "User-Agent: WemoPlug\r\n"
	   "Host: NILM\r\nAccept:*/*\r\n"
	   "Connection: keep-alive\r\n"
	   "Content-Type: application/json\r\n"
	   "Content-Length: %d\r\n"
	   "\r\n%s",
	   strlen(payload_buf),payload_buf);
  //send the packet!
  r = wifi_transmit(wemo_config.nilm_ip_addr,80,tx_buf);
  core_free(tx_buf);
  core_free(payload_buf);
  return r;
}
Exemple #2
0
////////////////////
//callback function
void core_get_nilm_ip_addr_cb(char* data){
  //find the IP address in the response
  //expect 4 octets enclosed by double brackets: [[x.x.x.]]
  int a1,a2,a3,a4;
  char *buf, *resp_data;
  int BUF_SIZE = MD_BUF_SIZE;
  buf = core_malloc(BUF_SIZE);
  resp_data = strrchr(data,'\n');
  if(sscanf(resp_data,"\n<<%d.%d.%d.%d>>",&a1,&a2,&a3,&a4)!=4){
    printf("error, bad address from manager\n");
    core_free(buf);
    return;
  }
  //save the IP to our config
  snprintf(buf,BUF_SIZE,"%d.%d.%d.%d",a1,a2,a3,a4);
  memset(wemo_config.nilm_ip_addr,0x0,MAX_CONFIG_LEN);
  strcpy(wemo_config.nilm_ip_addr,buf);
  //save the new config
  fs_write_config();

  //now send our IP to our NILM
  wifi_send_ip();
  //free the memory
  core_free(buf);
}
Exemple #3
0
//*** Private method that actually sends data ***//
int wifi_send_data(int ch, const uint8_t* data, int size){
  int cmd_buf_size = MD_BUF_SIZE;
  char *cmd;
  int timeout = 7; //wait 7 seconds to transmit the data
  //allocate memory
  cmd = core_malloc(cmd_buf_size);
  snprintf(cmd,cmd_buf_size,"AT+CIPSEND=%d,%d\r\n",ch,size);
  data_tx_status=TX_PENDING;
  rx_wait=true;
  resp_buf_idx = 0;
  memset(resp_buf,0x0,RESP_BUF_SIZE);
  memset(wifi_rx_buf,0x0,WIFI_RX_BUF_SIZE); //to make debugging easier
  wifi_rx_buf_idx=0;
  usart_serial_write_packet(WIFI_UART,(uint8_t*)cmd,strlen(cmd));
  delay_ms(250); //wait for module to be ready to accept data
  usart_serial_write_packet(WIFI_UART,(uint8_t*)data,size);
  //now wait for the data to be sent
  while(timeout>0){
    //start the timer
    tc_start(TC0, 0);	  
    //when timer expires, return what we have in the buffer
    rx_wait=true; //reset the wait flag
    while(rx_wait && data_tx_status!=TX_SUCCESS);
    tc_stop(TC0,0);
    //the success flag is set *before* we receive the server's response
    //core_process_wifi_data receives the response but discards it 
    if(data_tx_status==TX_SUCCESS){
      data_tx_status=TX_IDLE;
      rx_wait = false;
      //free memory
      core_free(cmd);
      return TX_SUCCESS;
    }
    timeout--;
  }
  //check if this is a timeout error
  if(strlen((char*)resp_buf)==0){
    printf("timeout error\n");
    core_log("timeout error");
    data_tx_status = TX_TIMEOUT;
  }
  //an error occured, see if it is due to a module reset
  else if(strcmp((char*)resp_buf,"\r\nready\r\n")==0){
    //module reset itself!!! 
    printf("detected module reset\n");
    core_log("module reset");
    data_tx_status = TX_ERR_MODULE_RESET;
  } else {
    data_tx_status=TX_ERROR;
    core_log("TX error:");
    core_log((char*)resp_buf);
    data_tx_status = TX_ERROR;
  }
  //free memory
  core_free(cmd);
  return data_tx_status;
}
Exemple #4
0
int
mon_log(int argc, char **argv){
  FIL fil;
  int line_buf_size = MD_BUF_SIZE;
  char *line_buf;
  FRESULT fr; 

  if(argc!=2){
    printf("specify [read] or [erase]\n");
    return -1;
  }
  if(strcmp(argv[1],"read")==0){
    //allocate the line buffer
    line_buf = core_malloc(line_buf_size);
    //print the log out to STDOUT
    fr = f_open(&fil, LOG_FILE, FA_READ);
    if(fr){
      printf("error reading log: %d\n",(int)fr);
      core_free(line_buf);
      return -1;
    }
    while(f_gets(line_buf, line_buf_size, &fil)){
      printf("%s",line_buf);
    }
    f_close(&fil);
    core_free(line_buf);
    return 0;
  }
  else if(strcmp(argv[1],"erase")==0){
    fr = f_open(&fil, LOG_FILE, FA_WRITE);
    if(fr){
      printf("error erasing log: %d\n", (int)fr);
      return -1;
    }
    f_lseek(&fil,0);
    f_truncate(&fil);
    f_close(&fil);
    if(wemo_config.echo)
      printf("erased log\n");
    return 0;
  }
  else{
    printf("specify [read] or [erase]\n");
    return -1;
  }
  //shouldn't get here
  return 0;
}
Exemple #5
0
static int shrink_callback(int handle, unsigned hints, void* start, size_t old_size)
{
    (void)start;
    (void)old_size;

    ssize_t old_size_s = old_size;
    size_t size_hint = (hints & BUFLIB_SHRINK_SIZE_MASK);
    ssize_t wanted_size = old_size_s - size_hint;

    if (wanted_size <= 0)
    {
        core_free(handle);
        buffering_reset(NULL, 0);
    }
    else
    {
        if (hints & BUFLIB_SHRINK_POS_FRONT)
            start += size_hint;

        buffering_reset(start, wanted_size);
        core_shrink(handle, start, wanted_size);
        buf = start;

        /* one-shot */
        add_event(BUFFER_EVENT_BUFFER_RESET, true, buffer_reset_handler);
    }

    return BUFLIB_CB_OK;
}
Exemple #6
0
static void plugin_release_audio_buffer(void)
{
    if (plugin_buffer_handle > 0)
    {
        plugin_buffer_handle = core_free(plugin_buffer_handle);
        plugin_buffer_size = 0;
    }
}
Exemple #7
0
int wifi_send_raw(int ch, const uint8_t* data, int size){
  int BUFFER_SIZE=1000;
  int i=0;
  int r;
  char *tx_buf;
  tx_buf = core_malloc(MD_BUF_SIZE);

  for(i=0;i<size;i+=BUFFER_SIZE){
    if(i+BUFFER_SIZE<size)
      r=wifi_send_data(ch,&data[i],BUFFER_SIZE);
    else
      r=wifi_send_data(ch,&data[i],size-i);
    if(r!=TX_SUCCESS){ //exit early
      core_free(tx_buf);
      return r; //fail!
    }
  }
  core_free(tx_buf);
  return r;
}
Exemple #8
0
int wifi_transmit(char *url, int port, char *data){
  int BUF_SIZE = MD_BUF_SIZE;
  char *cmd;
  char *buf;
  int r;
  //allocate memory
  cmd = core_malloc(BUF_SIZE);
  buf = core_malloc(BUF_SIZE);
  //sometimes the port stays open, so check for both
  //conditions
  char *success_str = "4,CONNECT";
  char *connected_str = "ALREAY CONNECT"; //sic
  //open a TCP connection on channel 4
  snprintf(cmd,BUF_SIZE,"AT+CIPSTART=4,\"TCP\",\"%s\",%d",
	  url,port);
  wifi_send_cmd(cmd,"4,CONNECT",buf,100,5);
  //check if we are able to connect to the NILM
  if(strstr(buf,"ERROR\r\nUnlink")==buf){
    printf("can't connect to NILM\n");
    core_free(cmd);
    core_free(buf);
    return TX_BAD_DEST_IP;
  }
  //if we are still connected, close and reopen socket
  if(strstr(buf,connected_str)==buf){
    wifi_send_cmd("AT+CIPCLOSE=4","Unlink",buf,100,1);
    //now try again
    wifi_send_cmd(cmd,"Linked",buf,100,2);
  }
  //check for successful link
  if(strstr(buf,success_str)!=buf){
    printf("error, setting up link\n");
    core_free(cmd);
    core_free(buf);
    return TX_ERROR;
  }
  //send the data
  if((r=wifi_send_txt(4,data))!=0){
    printf("error transmitting data: %d\n",data_tx_status);
    core_free(cmd);
    core_free(buf);
    return r;
  }
  //connection is closed *after* we receive the server's response
  //this is processed by the core and we discard the response
  //wifi_send_cmd("AT+CIPCLOSE=4","Unlink",buf,100,1);
  core_free(cmd);
  core_free(buf);
  return r; //success!
}
static void reset_shortcuts(void)
{
    int current_handle = first_handle;
    struct shortcut_handle *h = NULL;
    while (current_handle > 0)
    {
        int next;
        h = core_get_data(current_handle);
        next = h->next_handle;
        core_free(current_handle);
        current_handle = next;
    }
    first_handle = 0;
    shortcut_count = 0;
}   
Exemple #10
0
///////////////////
// core_get_nilm_ip_addr
//    Request the NILM IP address from the
//    manager, this allows us to operate in 
//    DHCP environments because the manager URL
//    can be resolved by DNS to a static IP
//
void
core_get_nilm_ip_addr(void){
  int BUF_SIZE=MD_BUF_SIZE;
  char *buf;
  //allocate memory
  buf=core_malloc(BUF_SIZE);
  snprintf(buf,BUF_SIZE,
	   "GET /nilms/get_ip?serial_number=%s HTTP/1.1\r\n"
	   "User-Agent: WemoPlug\r\n"
	   "Host: %s\r\n"
	   "Accept:*/*\r\n\r\n",
	   wemo_config.serial_number,wemo_config.mgr_url);
  wifi_transmit(wemo_config.mgr_url,80,buf);
  tx_callback = &core_get_nilm_ip_addr_cb;
  //free memory
  core_free(buf);
}
Exemple #11
0
void dsp_pbe_enable(int var)
{
    if (var == pbe_strength)
        return; /* No change */
    bool was_enabled = pbe_strength > 0;
    pbe_strength = var;

    bool now_enabled = var > 0;

    if (now_enabled == was_enabled)
        return; /* No change in enabled status */

    if (now_enabled == false && handle > 0)
    {
        core_free(handle);
        handle = -1;
    }

    struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO);
    dsp_proc_enable(dsp, DSP_PROC_PBE, now_enabled);
}
Exemple #12
0
int			main(int ac, char **av)
{
  t_core		core;
  struct termios	term;
  int			ret;

  init_core(&core, &term);
  if ((core.env = env_load()) == NULL)
    my_puterr(ERR_ENV);
  core.prompt = get_prompt(&core);
  init_term(&core, ac, av);
  core_config_file(&core);
  if (core.is_tcaps)
    enable_row_mode(core.term);
  if (core.is_tcaps && !(ret = core_run_termcap(&core)))
    return (ret);
  if (!core.is_tcaps && !(ret = core_run_no_termcap(&core)))
    return (ret);
  if (core.shutdown != ERR_SD_PIPE && core.is_tcaps)
    disable_row_mode(core.term);
  core_free(&core);
  return (core.shutdown == 1 ? 0 : core.shutdown);
}
Exemple #13
0
void dsp_surround_enable(int var)
{
    if (var == surround_strength)
        return; /* No setting change */

    bool was_enabled = surround_strength > 0;
    surround_strength = var;
    surround_set_stepsize(surround_strength);

    bool now_enabled = var > 0;

    if (was_enabled == now_enabled && !now_enabled)
        return; /* No change in enabled status */

    if (now_enabled == false && handle > 0)
    {
        core_free(handle);
        handle = -1;
    }
    surround_enabled = now_enabled;

    struct dsp_config *dsp = dsp_get_config(CODEC_IDX_AUDIO);
    dsp_proc_enable(dsp, DSP_PROC_SURROUND, now_enabled);
}
Exemple #14
0
void zmat_build(void)
{
gint i, j, k, n, type;
gdouble r, a, d, x[4][3], v[3];
gdouble  zaxis[3] = {0.0, 0.0, 1.0};
gchar *line;
GSList *list, *species;
struct zmat_pak *zmat;
struct core_pak *core[4] = {NULL, NULL, NULL, NULL};
struct model_pak *model;

model = sysenv.active_model;
if (!model)
  return;

/* CURRENT - using selection as our list of cores to generate a zmatrix from */
if (!model->selection)
  {
  gui_text_show(WARNING, "ZMATRIX: please select a molecule.\n");
  return;
  }

/* destroy old zmatrix */
/* TODO - prompt if non null */
zmat_free(model->zmatrix);
zmat = model->zmatrix = zmat_new();
zmat_angle_units_set(model->zmatrix, DEGREES);

/* setup SIESTA species type */
species = fdf_species_build(model);

/* sort the list so it follows molecular connectivity */
model->selection = zmat_connect_sort(model->selection);

n=0;
for (list=model->selection ; list ; list=g_slist_next(list))
  {
/* current atom/zmatrix line init */
  core[0] = list->data;
  type = fdf_species_index(core[0]->atom_label, species);
  line = NULL;

  zmat->zcores = g_slist_append(zmat->zcores, core[0]);

/* build a ZMATRIX line for processing */
  switch (n)
    {
    case 0:
      if (core[0])
        {
        ARR3SET(x[0], core[0]->x);
        vecmat(model->latmat, x[0]);
        }
      line = g_strdup_printf("%d  0 0 0  %f %f %f  0 0 0\n", type, x[0][0], x[0][1], x[0][2]);
      break;

    case 1:
      if (core[0])
        {
        ARR3SET(x[0], core[0]->x);
        vecmat(model->latmat, x[0]);
        }
      if (core[1])
        {
        ARR3SET(x[1], core[1]->x);
        vecmat(model->latmat, x[1]);
        }

      r = measure_distance(x[0], x[1]);

/* angle with z axis */
      ARR3SET(v, x[0]);
      ARR3SUB(v, x[1]);
      a = R2D * via(v, zaxis, 3);

/* angle between xy projection and x axis */
      d = R2D * angle_x_compute(v[0], v[1]);

      line = g_strdup_printf("%d  1 0 0  %f %f %f 0 0 0\n", type, r, a, d);
      break;

    case 2:
/* coords init */
  for (i=3 ; i-- ; )
    {
    if (core[i])
      {
      ARR3SET(x[i], core[i]->x);
      vecmat(model->latmat, x[i]);
      }
    else
      g_assert_not_reached();
    }

      r = measure_distance(x[0], x[1]);
      a = measure_angle(x[0], x[1], x[2]);

/* create a fake core -> 1 unit displaced in the z direction */
      g_assert(core[3] == NULL);
      core[3] = core_new("x", NULL, model);
      ARR3SET(core[3]->rx, core[2]->rx);
      ARR3ADD(core[3]->rx, zaxis); 
      d = measure_torsion(core);
      core_free(core[3]);

      line = g_strdup_printf("%d  2 1 0  %f %f %f 0 0 0\n", type,r,a,d);
      break;

    default:

/* connectivity test */
      if (!zmat_bond_check(core[0], core[1]))
        {
#if DEBUG_ZMAT_BUILD
printf("[%d] non-connected atoms [%f]\n", n, measure_distance(x[0], x[1]));
#endif
/* need to build a new connectivity chain starting from core[0] */
        core[1] = core[2] = core[3] = NULL;
        if (!zmat_connect_find(n, core, zmat))
          {
          gui_text_show(WARNING, "ZMATRIX: bad connectivity (molecule will be incomplete)\n");
          goto zmat_build_done;
          }
        }

/* coords init */
      for (i=3 ; i-- ; )
        {
        if (core[i])
          {
          ARR3SET(x[i], core[i]->x);
          vecmat(model->latmat, x[i]);
          }
        else
          g_assert_not_reached();
        }

      r = measure_distance(x[0], x[1]);
      a = measure_angle(x[0], x[1], x[2]);
      d = measure_torsion(core);

/* NB: indexing starts from 0, siesta starts from 1 (naturally) */
      i = 1+g_slist_index(zmat->zcores, core[1]);
      j = 1+g_slist_index(zmat->zcores, core[2]);
      k = 1+g_slist_index(zmat->zcores, core[3]);

      line = g_strdup_printf("%d  %d %d %d  %f %f %f 0 0 0\n", type,i,j,k,r,a,d);
    }

/* process a successfully constructed ZMATRIX line */
  if (line)
    {
    zmat_core_add(line, model->zmatrix);
    g_free(line);
    }

/* shuffle */
  core[3] = core[2];
  core[2] = core[1];
  core[1] = core[0];

  n++;
  }

zmat_build_done:

/* do the species typing */
zmat_type(model->zmatrix, species);

free_slist(species);
}
Exemple #15
0
int wifi_init(void){
  uint32_t BUF_SIZE = MD_BUF_SIZE;
  uint8_t tmp;  //dummy var for flushing UART
  int r; //return value from wifi_send_cmd (length of resp)
  char *buf;
  char *tx_buf;

  //allocate static buffers if they are not NULL
  if(resp_buf==NULL){
    resp_buf=core_malloc(RESP_BUF_SIZE);
  }
  if(resp_complete_buf==NULL){
    resp_complete_buf=core_malloc(RESP_COMPLETE_BUF_SIZE);
  }
  if(wifi_rx_buf==NULL){
    wifi_rx_buf = core_malloc(WIFI_RX_BUF_SIZE);
  }
  //if we are standalone, don't do anything
  if(wemo_config.standalone){
    printf("warning: wifi_init called in standalone mode\n");
    return 0; 
  }
  //initialize the memory
  buf = core_malloc(BUF_SIZE);
  tx_buf = core_malloc(BUF_SIZE);

  //set up the UART
  static usart_serial_options_t usart_options = {
    .baudrate = WIFI_UART_BAUDRATE,
    .charlength = WIFI_UART_CHAR_LENGTH,
    .paritytype = WIFI_UART_PARITY,
    .stopbits = WIFI_UART_STOP_BITS
  };
  gpio_configure_pin(PIO_PA9_IDX, (PIO_PERIPH_A | PIO_DEFAULT));
  gpio_configure_pin(PIO_PA10_IDX, (PIO_PERIPH_A | PIO_DEFAULT));
  pmc_enable_periph_clk(ID_WIFI_UART);
  sysclk_enable_peripheral_clock(ID_WIFI_UART);
  usart_serial_init(WIFI_UART,&usart_options);
  //flush any existing data
  while(usart_serial_is_rx_ready(WIFI_UART)){
    usart_serial_getchar(WIFI_UART,&tmp);
  }
  // Trigger from timer 0
  pmc_enable_periph_clk(ID_TC0);
  tc_init(TC0, 0,                        // channel 0
	  TC_CMR_TCCLKS_TIMER_CLOCK5     // source clock (CLOCK5 = Slow Clock)
	  | TC_CMR_CPCTRG                // up mode with automatic reset on RC match
	  | TC_CMR_WAVE                  // waveform mode
	  | TC_CMR_ACPA_CLEAR            // RA compare effect: clear
	  | TC_CMR_ACPC_SET              // RC compare effect: set
	  );
  TC0->TC_CHANNEL[0].TC_RA = 0;  // doesn't matter
  TC0->TC_CHANNEL[0].TC_RC = 64000;  // sets frequency: 32kHz/32000 = 1 Hz
  NVIC_ClearPendingIRQ(TC0_IRQn);
  NVIC_SetPriority(TC0_IRQn,1);//high priority
  NVIC_EnableIRQ(TC0_IRQn);
  tc_enable_interrupt(TC0, 0, TC_IER_CPCS);
  //reset the module
  if(wifi_send_cmd("AT+RST","ready",buf,BUF_SIZE,8)==0){
    printf("Error reseting ESP8266\n");
    //free memory
    core_free(buf);
    core_free(tx_buf);
    return -1;
  }
  //set to mode STA
  if(wifi_send_cmd("AT+CWMODE=1","OK",buf,BUF_SIZE,8)==0){
    printf("Error setting ESP8266 mode\n");
    core_free(buf);
    core_free(tx_buf);
    return 0;
  }
  //try to join the specified network  
  snprintf(tx_buf,BUF_SIZE,"AT+CWJAP=\"%s\",\"%s\"",
	   wemo_config.wifi_ssid,wemo_config.wifi_pwd);
  if((r=wifi_send_cmd(tx_buf,"OK",buf,BUF_SIZE,10))==0){
    printf("no response to CWJAP\n");
    //free memory
    core_free(buf);
    core_free(tx_buf);
    return -1;
  }
  //check for errors
  if( (r<1) || (strcmp(&buf[r-1],"OK")!=0)){
    snprintf(tx_buf,BUF_SIZE,"failed to join network [%s]: [%s]\n",wemo_config.wifi_ssid, buf);
    printf(tx_buf);
    core_log(tx_buf);
    //free memory
    core_free(buf);
    core_free(tx_buf);
    return -1;
  }
  //see if we have an IP address
  wifi_send_cmd("AT+CIFSR","OK",buf,BUF_SIZE,5);
  if(strstr(buf,"ERROR")==buf){
    printf("error getting IP address\n");
    //free the memory
    core_free(tx_buf);
    core_free(buf);
    return -1;
  }
  //try to parse the response into an IP address
  //expect 4 octets but *not* 0.0.0.0
  int a1,a2,a3,a4;
  if(!(sscanf(buf,"+CIFSR:STAIP,\"%d.%d.%d.%d\"",&a1,&a2,&a3,&a4)==4 && a1!=0)){
    printf("error, bad address: %s\n",buf);
    //free the memory
    core_free(tx_buf);
    core_free(buf);
    return -1;
  }
  //save the IP to our config
  snprintf(buf,BUF_SIZE,"%d.%d.%d.%d",a1,a2,a3,a4);
  memset(wemo_config.ip_addr,0x0,MAX_CONFIG_LEN);
  strcpy(wemo_config.ip_addr,buf);
  //set the mode to multiple connection
  wifi_send_cmd("AT+CIPMUX=1","OK",buf,BUF_SIZE,2);
  //start a server on port 1336
  wifi_send_cmd("AT+CIPSERVER=1,1336","OK",buf,BUF_SIZE,2);

  //if we know the NILM IP address, send it our IP
  if(strlen(wemo_config.nilm_ip_addr)!=0){
    if(wifi_send_ip()==TX_ERR_MODULE_RESET){
      return TX_ERR_MODULE_RESET;
    }
  } 
  else {
     //get the NILM IP address from the manager
     //once we know the NILM address we send it ours
     core_get_nilm_ip_addr();
  }

  //log the event
  snprintf(buf,BUF_SIZE,"Joined [%s] with IP [%s]",
	   wemo_config.wifi_ssid,wemo_config.ip_addr);
  printf("\n%s\n",buf);
  core_log(buf);
  //free the memory
  core_free(tx_buf);
  core_free(buf);
  return 0;
}
Exemple #16
0
	static void operator delete[](void * mem,size_t size)
	{
		core_free(mem,size);
	}
Exemple #17
0
void core_transmit_power_packet_http(power_pkt *wemo_pkt){
  int r, i, j;
  int TX_BUF_SIZE = XL_BUF_SIZE;
  int PAYLOAD_BUF_SIZE = XL_BUF_SIZE;
  int DATA_BUF_SIZE = MD_BUF_SIZE;
  char *tx_buf, *payload_buf;
  char *bufs[7];
  int32_t *srcs[7] = {wemo_pkt->vrms, wemo_pkt->irms, wemo_pkt->pavg, wemo_pkt->watts,
		    wemo_pkt->pf, wemo_pkt->freq, wemo_pkt->kwh};
  char *buf; int32_t *src;
  
  //make sure all the data is present
  if(wemo_pkt->status != POWER_PKT_READY){
    printf("Error, packet is not ready to tx!\n");
    return;
  }
  //now we are filling up the POST request
  wemo_pkt->status = POWER_PKT_TX_IN_PROG;

  //allocate memory for the POST request
  tx_buf = core_malloc(TX_BUF_SIZE);
  payload_buf = core_malloc(DATA_BUF_SIZE);

  //print out the data arrays as strings
  for(j=0;j<7;j++){
    src = srcs[j];
    buf = core_malloc(DATA_BUF_SIZE);
    bufs[j] = buf;
    for(i=0;i<PKT_SIZE;i++){
      snprintf(buf+strlen(buf),DATA_BUF_SIZE,"%li",src[i]);
      if(i<(PKT_SIZE-1))
	snprintf(buf+strlen(buf),DATA_BUF_SIZE,", ");
    }
  }
  //stick them in a json format
  snprintf(payload_buf,PAYLOAD_BUF_SIZE,
	   "{\"plug\":\"%s\",\"ip\":\"%s\",\"time\":\"%s\","
	   "\"vrms\":[%s],\"irms\":[%s],\"watts\":[%s],\"pavg\":[%s],"
	   "\"pf\":[%s],\"freq\":[%s],\"kwh\":[%s]}",
	   wemo_config.serial_number,wemo_config.ip_addr,wemo_pkt->timestamp,
	   bufs[0],bufs[1],bufs[2],bufs[3],bufs[4],bufs[5],bufs[6]);
  snprintf(tx_buf,TX_BUF_SIZE,
	     "POST /config/plugs/log HTTP/1.1\r\n"
	     "User-Agent: WemoPlug\r\n"
	     "Host: NILM\r\nAccept:*/*\r\n"
	     "Connection: keep-alive\r\n"
	     "Content-Type: application/json\r\n"
	     "Content-Length: %d\r\n"
	     "\r\n%s",
	     strlen(payload_buf),payload_buf);
  //send the packet!
  r = wifi_transmit(wemo_config.nilm_ip_addr,80,tx_buf);
  switch(r){
  case TX_SUCCESS:
    wemo_pkt->status = POWER_PKT_EMPTY;
    break;
  case TX_BAD_DEST_IP: //if we can't find NILM, get its IP from the manager
    wemo_pkt->status = POWER_PKT_EMPTY; //just dump the packet
    core_get_nilm_ip_addr();
    break;
  case TX_ERR_MODULE_RESET:
    wifi_init(); //re-initialize WiFi on reset error
  case TX_ERROR: //just fall through and register the failure
  case TX_TIMEOUT:
  default:
    wemo_pkt->status = POWER_PKT_TX_FAIL;
  }
  //free memory
  core_free(tx_buf);
  core_free(payload_buf);
  for(j=0;j<7;j++){
    core_free(bufs[j]);
  }
}
Exemple #18
0
void core_process_wifi_data(void){
  int BUF_SIZE=XL_BUF_SIZE;
  char *buf;
  int chan_num, data_size, r;
  unsigned int red,green,blue, blink;
  unsigned int yr,mo,dt,dw,hr,mn,sc;
  int time_buf_size =  MD_BUF_SIZE;
  char *time_buf;

  //match against the data
  if(strlen(wifi_rx_buf)>BUF_SIZE){
    printf("can't process rx'd packet, too large\n");
    core_log("can't process rx'd packet, too large");
    return;
  }
  //allocate memory
  buf = core_malloc(BUF_SIZE);
  r=sscanf(wifi_rx_buf,"\r\n+IPD,%d,%d:%s", &chan_num, &data_size, buf);
  if(r!=3){
    printf("rx'd corrupt data, ignoring\n");
    core_log("rx'd corrupt data, ignoring\n");
    //free memory
    core_free(buf);
    return;
  }

  if(wemo_config.debug_level>DEBUG_INFO)
    printf("Got [%d] bytes on channel [%d]\n",
	   data_size, chan_num);
  //discard responses from the NILM to power logging packets, but keep the response
  //if another core function has requested some data, this is done with the callback 
  //function. The requesting core function registers a callback and this function calls
  //it and then resets the callback to NULL
  if(chan_num==4){
    //close the socket
    wifi_send_cmd("AT+CIPCLOSE=4","Unlink",buf,BUF_SIZE,1);
    //execute the callback
    if(tx_callback!=NULL){
      (*tx_callback)(wifi_rx_buf);
      tx_callback=NULL;
    }
    //clear the server buffer
    memset(wifi_rx_buf,0x0,WIFI_RX_BUF_SIZE);
    //free memory
    core_free(buf);
    return;
  }
  ///////////////////
  //this data must be inbound to the server port, process the command
  //
  //     RELAY ON
  if(strcmp(buf,"relay_on")==0){
    gpio_set_pin_high(RELAY_PIN);
    printf("relay ON\n");
    //return "OK" to indicate success
    wifi_send_txt(0,"OK");
  }
  //     RELAY OFF
  else if(strcmp(buf,"relay_off")==0){
    gpio_set_pin_low(RELAY_PIN);
    printf("relay OFF\n");
    //return "OK" to indicate success
    wifi_send_txt(0,"OK");
  }
  //     LED SET
  else if(strstr(buf,"set_led")==buf){
    if(sscanf(buf,"set_led_%u_%u_%u_%u.",&red,&green,&blue,&blink)!=4){
      core_log("corrupt led_set request");
    } else {
      rgb_led_set(red,green,blue,blink);
      if(wemo_config.echo)
	printf("set led: [%u, %u, %u, %u]\n",red,green,blue,blink);
      wifi_send_txt(0,"OK");
    }
  }
  //     RTC SET
  else if(strstr(buf,"set_rtc")==buf){
    if(sscanf(buf,"set_rtc_%u_%u_%u_%u_%u_%u_%u.",
	      &yr,&mo,&dt,&dw,&hr,&mn,&sc)!=7){
      core_log("corrupt rtc_set request");
    } else {
      if(i2c_rtc_set_time(sc,mn,hr,dw,dt,mo,yr)!=0)
	printf("error setting RTC\n");
      else{
	time_buf = membag_alloc(time_buf_size);
	if(time_buf==NULL)
	  core_panic();
	rtc_get_time_str(time_buf,time_buf_size);
	if(wemo_config.echo)
	  printf("wifi set rtc to: %s\n",time_buf);
	core_log("wifi set rtc");
	membag_free(time_buf);
	wifi_send_txt(0,"OK");
      }
    }
  }
  //     SEND DATA
  else if(strcmp(buf,"send_data")==0){
    if(tx_pkt->status!=POWER_PKT_READY){
      r = wifi_send_txt(chan_num,"error: no data");
      if(r==TX_ERR_MODULE_RESET)
	while(wifi_init()!=0); //fail!!! anger!!!! reset the module
    } else {
      //send the data
      r=wifi_send_raw(chan_num,(uint8_t*)tx_pkt,sizeof(*tx_pkt));
      if(r==TX_ERR_MODULE_RESET){
	while(wifi_init()!=0); //fail!! anger!!! reset the module
      } else {
	//clear out the packet so we can start again
	memset(tx_pkt,0,sizeof(*tx_pkt));
	tx_pkt->status=POWER_PKT_EMPTY;
	if(wemo_config.debug_level>=DEBUG_INFO)
	  printf("sent data\n");
      }
    }
  }
  else{
    printf("unknown command: %s\n",buf);
    wifi_send_txt(chan_num,"error: unknown command");
    //free memory
    core_free(buf);
    return;
  }
  //clear the server buffer
  memset(wifi_rx_buf,0x0,WIFI_RX_BUF_SIZE);
  //free the memory
  core_free(buf);
  return;
}
Exemple #19
0
int plugin_load(const char* plugin, const void* parameter)
{
    struct plugin_header *p_hdr;
    struct lc_header     *hdr;

    if (current_plugin_handle && pfn_tsr_exit)
    {    /* if we have a resident old plugin and a callback */
        if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
        {
            /* not allowing another plugin to load */
            return PLUGIN_OK;
        }
        lc_close(current_plugin_handle);
        current_plugin_handle = pfn_tsr_exit = NULL;
        if (plugin_buffer_handle > 0)
            plugin_buffer_handle = core_free(plugin_buffer_handle);
    }

    splash(0, ID2P(LANG_WAIT));
    strcpy(current_plugin, plugin);

    current_plugin_handle = lc_open(plugin, pluginbuf, PLUGIN_BUFFER_SIZE);
    if (current_plugin_handle == NULL) {
        splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
        return -1;
    }

    p_hdr = lc_get_header(current_plugin_handle);

    hdr = p_hdr ? &p_hdr->lc_hdr : NULL;
    

    if (hdr == NULL
        || hdr->magic != PLUGIN_MAGIC
        || hdr->target_id != TARGET_ID
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
        || hdr->load_addr != pluginbuf
        || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE
#endif
        )
    {
        lc_close(current_plugin_handle);
        splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
        return -1;
    }
    if (hdr->api_version > PLUGIN_API_VERSION
        || hdr->api_version < PLUGIN_MIN_API_VERSION)
    {
        lc_close(current_plugin_handle);
        splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
        return -1;
    }
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
    plugin_size = hdr->end_addr - pluginbuf;
#else
    plugin_size = 0;
#endif

    *(p_hdr->api) = &rockbox_api;

    lcd_clear_display();
    lcd_update();

#ifdef HAVE_REMOTE_LCD
    lcd_remote_clear_display();
    lcd_remote_update();
#endif
    push_current_activity(ACTIVITY_PLUGIN);
    /* some plugins assume the entry cache doesn't move and save pointers to it
     * they should be fixed properly instead of this lock */
    tree_lock_cache(tree_get_context());

    FOR_NB_SCREENS(i)
       viewportmanager_theme_enable(i, false, NULL);
    
#ifdef HAVE_TOUCHSCREEN
    touchscreen_set_mode(TOUCHSCREEN_BUTTON);
#endif

    /* allow voice to back off if the plugin needs lots of memory */
    talk_buffer_set_policy(TALK_BUFFER_LOOSE);

    plugin_check_open_close__enter();

    int rc = p_hdr->entry_point(parameter);
    
    tree_unlock_cache(tree_get_context());
    pop_current_activity();

    if (!pfn_tsr_exit)
    {   /* close handle if plugin is no tsr one */
        lc_close(current_plugin_handle);
        current_plugin_handle = NULL;
        if (plugin_buffer_handle > 0)
            plugin_buffer_handle = core_free(plugin_buffer_handle);
    }

    talk_buffer_set_policy(TALK_BUFFER_DEFAULT);

    /* Go back to the global setting in case the plugin changed it */
#ifdef HAVE_TOUCHSCREEN
    touchscreen_set_mode(global_settings.touch_mode);
#endif

#ifdef HAVE_LCD_BITMAP
    screen_helper_setfont(FONT_UI);
#if LCD_DEPTH > 1
#ifdef HAVE_LCD_COLOR
    lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
                                   global_settings.bg_color);
#else
    lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
#endif
#else /* LCD_DEPTH == 1 */
    lcd_set_drawmode(DRMODE_SOLID);
#endif /* LCD_DEPTH */
#endif /* HAVE_LCD_BITMAP */


#ifdef HAVE_REMOTE_LCD
#if LCD_REMOTE_DEPTH > 1
    lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
                            LCD_REMOTE_DEFAULT_BG);
#else
    lcd_remote_set_drawmode(DRMODE_SOLID);
#endif
#endif

    lcd_clear_display();
#ifdef HAVE_REMOTE_LCD
    lcd_remote_clear_display();
#endif

    FOR_NB_SCREENS(i)
        viewportmanager_theme_undo(i, true);

    plugin_check_open_close__exit();

    if (rc == PLUGIN_ERROR)
        splash(HZ*2, str(LANG_PLUGIN_ERROR));

    return rc;
}
Exemple #20
0
/** free_pattern
  *
  * Deallocate the memory used by a pattern object. Don't free
  * the regex string, since that will be freed in the hash table.
  */
static void free_pattern(pattern_t* pattern) {
   obhash_free(pattern->names);
   core_free(pattern->core);
   free(pattern);
}