Пример #1
0
Cell* consolefs_read() {
  int c = fgetc(stdin);
  if (c==13) c=10; // CR/LF
  Cell* str = alloc_string_copy(" ");
  ((char*)str->addr)[0] = c;
  return str;
}
Пример #2
0
Cell* keyfs_read() {
  SDL_Event event;
  if (SDL_PollEvent(&event)) 
  {
    //printf("sdl event! %d\n",event.type);
    
    switch (event.type) 
    {
    case SDL_QUIT:
      exit(0);
      break;
    case SDL_KEYDOWN:
      sdl_modifiers = event.key.keysym.mod;
      sdl_key = event.key.keysym.sym;
      break;
    }
  }

  switch (sdl_key) {
  case 13: sdl_key = 10; break;
  case 8: sdl_key = 127; break;
  }
  
  Cell* res = alloc_string_copy(" ");
  ((uint8_t*)res->addr)[0] = sdl_key;
  sdl_key = 0;
  return res;
}
Пример #3
0
Cell* fbfs_read(Cell* stream) {
  Stream* s = (Stream*)stream->ar.addr;
  char* path = s->path->ar.addr;
  if (!strcmp(path+12,"/width")) {
    return alloc_int(WIDTH);
  }
  else if (!strcmp(path+12,"/height")) {
    return alloc_int(HEIGHT);
  }
  else if (!strcmp(path+12,"/depth")) {
    return alloc_int(BPP);
  }
  else if (!strcmp(path+12,"/")) {
    return
      alloc_cons(alloc_string_copy("/width"),
      alloc_cons(alloc_string_copy("/height"),
      alloc_cons(alloc_string_copy("/depth"),alloc_nil())));
  }
  else {
    return alloc_int(0);
  }
}
Пример #4
0
sn_coap_hdr_s* M2MObject::handle_put_request(nsdl_s *nsdl,
                                             sn_coap_hdr_s *received_coap_header,
                                             M2MObservationHandler */*observation_handler*/,
                                             bool &/*execute_value_updated*/)
{
    tr_debug("M2MObject::handle_put_request()");
    sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
    sn_coap_hdr_s *coap_response = sn_nsdl_build_response(nsdl,
                                                          received_coap_header,
                                                          msg_code);
    if(received_coap_header) {
        if(received_coap_header->options_list_ptr &&
           received_coap_header->options_list_ptr->uri_query_ptr) {
            char *query = (char*)alloc_string_copy(received_coap_header->options_list_ptr->uri_query_ptr,
                                                    received_coap_header->options_list_ptr->uri_query_len);
            if (query){
                tr_debug("M2MObject::handle_put_request() - Query %s", query);
                // if anything was updated, re-initialize the stored notification attributes
                if (!handle_observation_attribute(query)){
                    tr_debug("M2MObject::handle_put_request() - Invalid query");
                    msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
                }
                free(query);
            }
        } else {
            tr_error("M2MObject::handle_put_request() - COAP_MSG_CODE_RESPONSE_BAD_REQUEST - Empty URI_QUERY");
            msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
        }
    } else {
        msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
    }
    if(coap_response) {
        coap_response->msg_code = msg_code;
    }
    return coap_response;
}
Пример #5
0
sn_coap_hdr_s* M2MResource::handle_post_request(nsdl_s *nsdl,
                                                sn_coap_hdr_s *received_coap_header,
                                                M2MObservationHandler */*observation_handler*/,
                                                bool &/*execute_value_updated*/)
{
    tr_debug("M2MResource::handle_post_request()");
    sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
    sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
                                                           received_coap_header,
                                                           msg_code);
    // process the POST if we have registered a callback for it
    if(received_coap_header) {
        if ((operation() & SN_GRS_POST_ALLOWED) != 0) {
            M2MResource::M2MExecuteParameter *exec_params = new M2MResource::M2MExecuteParameter();
            if (exec_params) {
                exec_params->_object_name = object_name();
                exec_params->_resource_name = name();
                exec_params->_object_instance_id = object_instance_id();
            }
            uint16_t coap_content_type = 0;
            if(received_coap_header->payload_ptr) {
                if(received_coap_header->content_type_ptr) {
                    for(uint8_t i = 0; i < received_coap_header->content_type_len; i++) {
                        coap_content_type = (coap_content_type << 8) + (received_coap_header->content_type_ptr[i] & 0xFF);
                    }
                }
                if(coap_content_type == 0) {
                    if (exec_params){
                        exec_params->_value = alloc_string_copy(received_coap_header->payload_ptr,
                                                                received_coap_header->payload_len);
                        if (exec_params->_value) {
                            exec_params->_value_length = received_coap_header->payload_len;
                        }
                    }
                } else {
                    msg_code = COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT;
                }
            }
            if(COAP_MSG_CODE_RESPONSE_CHANGED == msg_code) {
                tr_debug("M2MResource::handle_post_request - Execute resource function");
                execute(exec_params);
                if(_delayed_response) {
                    coap_response->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
                    coap_response->msg_code = COAP_MSG_CODE_EMPTY;
                    coap_response->msg_id = received_coap_header->msg_id;
                    if(received_coap_header->token_len) {
                        free(_delayed_token);
                        _delayed_token_len = 0;

                        _delayed_token = alloc_copy(received_coap_header->token_ptr, _delayed_token_len);
                        if(_delayed_token) {
                            _delayed_token_len = received_coap_header->token_len;
                        }
                    }
                } else {
                    uint32_t length = 0;
                    get_value(coap_response->payload_ptr, length);
                    coap_response->payload_len = length;
                }
            }
            delete exec_params;
        } else { // if ((object->operation() & SN_GRS_POST_ALLOWED) != 0)
            tr_error("M2MResource::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
            msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.05
        }
    } else { //if(object && received_coap_header)
        tr_error("M2MResource::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
        msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.01
    }
    if(coap_response) {
        coap_response->msg_code = msg_code;
    }
    return coap_response;
}
Пример #6
0
sn_coap_hdr_s* M2MResource::handle_put_request(nsdl_s *nsdl,
                                               sn_coap_hdr_s *received_coap_header,
                                               M2MObservationHandler *observation_handler,
                                               bool &execute_value_updated)
{
    tr_debug("M2MResource::handle_put_request()");
    sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
    sn_coap_hdr_s * coap_response = NULL;
    if(_has_multiple_instances) {
        coap_response = sn_nsdl_build_response(nsdl,
                                               received_coap_header,
                                               msg_code);
        // process the PUT if we have registered a callback for it
        if(received_coap_header) {
            uint16_t coap_content_type = 0;
            bool content_type_present = false;
            if(received_coap_header->content_type_ptr) {
                if(coap_response) {
                    content_type_present = true;
                    coap_response->content_type_ptr = alloc_copy(received_coap_header->content_type_ptr,
                                                                    received_coap_header->content_type_len);
                    if(coap_response->content_type_ptr) {
                        coap_response->content_type_len = received_coap_header->content_type_len;
                        for(uint8_t i = 0; i < coap_response->content_type_len; i++) {
                            coap_content_type = (coap_content_type << 8) +
                                    (coap_response->content_type_ptr[i] & 0xFF);
                        }
                    }
                }
            }
            if(received_coap_header->options_list_ptr &&
               received_coap_header->options_list_ptr->uri_query_ptr) {
                char *query = (char*)alloc_string_copy(received_coap_header->options_list_ptr->uri_query_ptr,
                                                        received_coap_header->options_list_ptr->uri_query_len);
                if (query){
                    msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
                    tr_debug("M2MResource::handle_put_request() - Query %s", query);
                    // if anything was updated, re-initialize the stored notification attributes
                    if (!handle_observation_attribute(query)){
                        tr_debug("M2MResource::handle_put_request() - Invalid query");
                        msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
                    }
                    free(query);
                }
            } else if ((operation() & SN_GRS_PUT_ALLOWED) != 0) {
                if(!content_type_present &&
                   M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE) {
                    coap_content_type = COAP_CONTENT_OMA_TLV_TYPE;
                }

                tr_debug("M2MResource::handle_put_request() - Request Content-Type %d", coap_content_type);

                if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type) {
                    M2MTLVDeserializer *deserializer = new M2MTLVDeserializer();
                    if(deserializer) {
                        M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
                        error = deserializer->deserialize_resource_instances(received_coap_header->payload_ptr,
                                                                             received_coap_header->payload_len,
                                                                             *this,
                                                                             M2MTLVDeserializer::Put);
                        switch(error) {
                            case M2MTLVDeserializer::None:
                                if(observation_handler) {
                                    String value = "";
                                    if (received_coap_header->uri_path_ptr != NULL &&
                                        received_coap_header->uri_path_len > 0) {

                                        value.append_raw((char*)received_coap_header->uri_path_ptr,received_coap_header->uri_path_len);
                                    }
                                    execute_value_updated = true;
                                }
                                msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
                                break;
                            case M2MTLVDeserializer::NotFound:
                                msg_code = COAP_MSG_CODE_RESPONSE_NOT_FOUND;
                                break;
                            case M2MTLVDeserializer::NotAllowed:
                                msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
                                break;
                            case M2MTLVDeserializer::NotValid:
                                msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
                                break;
                        }
                        delete deserializer;
                    }
                } else {
                    msg_code =COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT;
                } // if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type)
            } else {
                // Operation is not allowed.
                tr_error("M2MResource::handle_put_request() - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
                msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
            }
        } else {
            msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
        }
        if(coap_response) {
            coap_response->msg_code = msg_code;
        }
    } else {        
        coap_response = M2MResourceInstance::handle_put_request(nsdl,
                            received_coap_header,
                            observation_handler,
                            execute_value_updated);
    }
    return coap_response;
}
Пример #7
0
Cell* keyfs_read() {
  sdl_key = 0;
  SDL_Event event;
  
  if (SDL_PollEvent(&event))
  {
    //printf("sdl event! %d\n",event.type);
    
    switch (event.type) 
    {
    case SDL_QUIT:
      exit(0);
      break;
    case SDL_TEXTINPUT:
    case SDL_KEYDOWN:
      if (event.type == SDL_KEYDOWN) {
        sdl_modifiers = event.key.keysym.mod;
        //printf("key: %d, mod: %x\r\n",event.key.keysym.sym,event.key.keysym.mod);
        sdl_key = event.key.keysym.sym;
      } else {
        sdl_modifiers = 0;
        sdl_key = event.text.text[0];
      }
      
      if (sdl_key<200) {
      } else {
        switch (sdl_key) {
          case 1073741906: sdl_key = 17; break; // DC1 cursor up
          case 1073741905: sdl_key = 18; break; // DC2 cursor down
          case 1073741904: sdl_key = 19; break; // DC3 cursor left
          case 1073741903: sdl_key = 20; break; // DC4 cursor right
          default: sdl_key = 0;
        }
      }
      if (sdl_modifiers&1 || sdl_modifiers&2) {
        if (sdl_key>='a' && sdl_key<='z') {
          sdl_key+=('A'-'a');
        }
        else {
          switch (sdl_key) {
          case 223: sdl_key = '?'; break;
          case '1': sdl_key = '!'; break;
          case '2': sdl_key = '"'; break;
          case '3': sdl_key = '~'; break;
          case '4': sdl_key = '$'; break;
          case '5': sdl_key = '%'; break;
          case '6': sdl_key = '&'; break;
          case '7': sdl_key = '/'; break;
          case '8': sdl_key = '('; break;
          case '9': sdl_key = ')'; break;
          case '0': sdl_key = '='; break;
          case '<': sdl_key = '>'; break;
          case '+': sdl_key = '*'; break;
          case '#': sdl_key = '\''; break;
          case ',': sdl_key = ';'; break;
          case '.': sdl_key = ':'; break;
          case '-': sdl_key = '_'; break;
          default: break;
          }
        }
      }
      break;
    case SDL_MOUSEMOTION:
      mouse_x = event.motion.x;
      mouse_y = event.motion.y;
      mouse_buttons = event.motion.state;
      break;
    }
  }

  switch (sdl_key) {
  case 13: sdl_key = 10; break;
  case 8: sdl_key = 127; break;
  }
  
  Cell* res = alloc_string_copy(" ");
  ((uint8_t*)res->ar.addr)[0] = sdl_key;
  sdl_key = 0;
  return res;
}
Пример #8
0
Cell* posixfs_open(Cell* cpath) {
  char* path;
  _file_cell = alloc_nil();

  if (!cpath || cpath->tag!=TAG_STR) {
    printf("[posixfs] open error: non-string path given\r\n");
    return _file_cell;
  }

  path = cpath->ar.addr;
  
  if (!strncmp(path,"/sd/",4)) {
    char* name = NULL;
    char* filename = NULL;

    if (strlen(path)>4) {
      filename = path+4;
    }

    if (!filename || !filename[0]) filename = ".";

    printf("filename: %s\r\n",filename);
    
    if (filename) {
      struct stat src_stat;
      DIR* dirp;
      int f;
      off_t len;
      
      if (stat(filename, &src_stat)) {
        _file_cell = alloc_string_copy("<file not found>");
        return _file_cell;
      }
      len = src_stat.st_size;

      if ((dirp = opendir(filename))) {
        struct dirent *dp;
        Cell* nl = alloc_string_copy("\n");
        _file_cell = alloc_string_copy("");
        
        do {
          if ((dp = readdir(dirp)) != NULL) {
            printf("dp: |%s|\r\n",dp->d_name);
            _file_cell = alloc_concat(_file_cell,alloc_concat(alloc_string_copy(dp->d_name),nl));
          }
        } while (dp != NULL);
        return _file_cell;
      }

      f = open(filename, O_RDONLY);
      if (f>-1) {
        Cell* res;
        int read_len;
        
        printf("[posixfs] trying to read file of len %d...\r\n",len);
        res = alloc_num_bytes(len);
        read_len = read(f, res->ar.addr, len);
        close(f);
        // TODO: close?
        _file_cell = res;
        return res;
      } else {
        // TODO should return error
        printf("[posixfs] could not open file :(\r\n");
        _file_cell = alloc_string_copy("<error: couldn't open file.>"); // FIXME hack
        return _file_cell;
      }
      _file_cell = alloc_string_copy("<error: file not found.>");
      return _file_cell;
    } else {
      // TODO dir
    }
  }

  return _file_cell;
}