//-------------------------------------------------------- static int8_t process_input(over_thresh_state_t *s) { uint8_t overTheThresh; objectInfo newObject; if (s->state < 4) { return -EBUSY; } s->state = 0; overTheThresh = over_thresh(s->A, s->row, s->col, RANGE, s->thresh); newObject.objectPosition.x = s->col; newObject.objectPosition.y = s->row; newObject.objectSize.x = ROWS; newObject.objectSize.y = COLS; newObject.actionFlag = NONE; if (overTheThresh > DETECT_THRESH) { LED_DBG(LED_RED_TOGGLE); newObject.actionFlag = OBJ_DETECT; } token_type_t *my_token = create_token(&newObject, sizeof(objectInfo), s->pid); if (my_token == NULL) return -ENOMEM; //SOS_CALL(s->put_token, put_token_func_t, s->output0, my_token); dispatch(s->output0, my_token); destroy_token(my_token); //destroy_cyclops_matrix(s->A); destroy_token_data(s->A, CYCLOPS_MATRIX, sizeof(CYCLOPS_Matrix)); s->A = NULL; return SOS_OK; }
static int8_t element_module(void *state, Message *msg) { element_state_t *s = (element_state_t *)state; switch (msg->type) { case MSG_INIT: { // Default value = 9 seconds. s->computation_time = 9; // Default: 3 LSBits s->bit_mask = 0x07; s->pid = msg->did; break; } case MSG_TIMER_TIMEOUT: { DEBUG("Truncate_long timer fired. Output the old token.\n"); token_type_t *my_token = create_token(&s->value, sizeof(uint8_t), s->pid); if (my_token == NULL) { SOS_CALL(s->signal_error, signal_error_func_t, -ENOMEM); return -EINVAL; } //SOS_CALL(s->put_token, put_token_func_t, s->output0, my_token); dispatch(s->output0, my_token); destroy_token(my_token); break; } case MSG_FINAL: { break; } default: return -EINVAL; } return SOS_OK; }
//-------------------------------------------------------- //static int8_t input0 (func_cb_ptr p, void *data, uint16_t length) { static int8_t input0 (func_cb_ptr p, token_type_t *t) { update_background_state_t *s = (update_background_state_t *)sys_get_state(); // Get token from port. if (s->state == UPD_STATE_INIT) { s->backMat = (CYCLOPS_Matrix *)capture_token_data(t, s->pid); if (s->backMat == NULL) return -ENOMEM; s->state = UPD_STATE_PROCESS; } else { CYCLOPS_Matrix *M = (CYCLOPS_Matrix *)get_token_data(t); //check that input matrix's depth is 1 byte if( (M->depth != CYCLOPS_1BYTE) || (s->backMat->depth != CYCLOPS_1BYTE) ) return -EINVAL; token_type_t *my_token = create_token(s->backMat, sizeof(CYCLOPS_Matrix), s->pid); if (my_token == NULL) return -EINVAL; set_token_type(my_token, CYCLOPS_MATRIX); //SOS_CALL(s->put_token, put_token_func_t, s->output0, my_token); dispatch(s->output0, my_token); destroy_token(my_token); } return SOS_OK; }
void inner_get_token(char **str, t_token *new_token) { if (*str != new_token->start) new_token->type = INTEGER; else if ((**str) == '(') new_token->type = LEFT_PAR; else if ((**str) == ')') new_token->type = RIGHT_PAR; else if ((**str) == '+') new_token->type = PLUS; else if ((**str) == '*') new_token->type = MULT; else if ((**str) == '\0') new_token->type = END_OF_INPUT; else destroy_token(&new_token); }
static int8_t input0 (func_cb_ptr p, token_type_t *t) { element_state_t *s = (element_state_t *)sys_get_state(); // Get token from port. // If want to break the chain of calls here, then copy the token into a private // data structure(global), and return appropriate value (SOS_OK). // Process input: Extract 3 LSB's and pass it on to the next function. // We need a separate place to hold the output as we are modifying the input. // Remember, this module does not own the input token, so should not // overwrite it. uint8_t out_value = (*((uint8_t *)get_token_data(t))) & s->bit_mask; token_type_t *my_token = create_token(&out_value, sizeof(uint8_t), s->pid); if (my_token == NULL) return -ENOMEM; //SOS_CALL(s->put_token, put_token_func_t, s->output0, my_token); dispatch(s->output0, my_token); destroy_token(my_token); return SOS_OK; }
static int8_t process_input(element_state_t *s, uint8_t data) { uint8_t out_value; // Get token from port. // If want to break the chain of calls here, then copy the token into a private // data structure(global), and return appropriate value (-EBUSY). switch (s->status) { case IDLE: { s->stored_input[0] = data; s->status = WAITING_FOR_1; return -EBUSY; } case WAITING_FOR_1: { s->stored_input[1] = data; s->status = WAITING_FOR_2; return -EBUSY; } case WAITING_FOR_2: { // Process input: Combine (OR) the three inputs and pass the result on to the next function. // We need a separate place to hold the output as we are modifying the input. // Remember, this module does not own the input token, so should not // overwrite it. out_value = data | s->stored_input[0] | s->stored_input[1]; DEBUG("COMBINE: Output - token %d.\n", out_value); token_type_t *my_token = create_token(&out_value, sizeof(uint8_t), s->pid); if (my_token == NULL) return -ENOMEM; //SOS_CALL(s->put_token, put_token_func_t, s->output0, my_token); dispatch(s->output0, my_token); s->status = IDLE; destroy_token(my_token); return SOS_OK; } default: return SOS_OK; } return SOS_OK; }
// tokenizes the string str Ttoken *tokenize(char *str) { Ttoken *tok_list, *tok_tmp = NULL; char word[256]; int a, b, c; int i = 0; tok_list = create_token("head"); while(str[i] != '\0') { // skip leading whitespace while(str[i] == ' ' || str[i] == '\t') i++; if (str[i] == '"') { // find end of string part i ++; a = i; while(str[i] != '"') i++; b = i; i ++; } else { // find end of word a = i; while(str[i] != ' ' && str[i] != '\t' && str[i] != '\0') i++; b = i; } // copy string to word for(c=0;c<b-a;c++) word[c] = str[a+c]; word[c] = '\0'; // make token and put it in the list tok_tmp = create_token(word); insert_token(tok_list, tok_tmp); } tok_tmp->word[strlen(tok_tmp->word) - 1] = '\0'; tok_tmp = (Ttoken *)tok_list->next; destroy_token(tok_list); return tok_tmp; }
static int8_t element_module(void *state, Message *msg) { element_state_t *s = (element_state_t *)state; switch (msg->type) { case MSG_INIT: { LED_DBG(LED_RED_ON); s->cnt = 0; // Default parameter value = 5 sec. s->sample_rate_in_sec = 5; s->pid = msg->did; sys_timer_start(TIMER_TID, ((uint32_t)s->sample_rate_in_sec) * 1024L, TIMER_ONE_SHOT); break; } case MSG_TIMER_TIMEOUT: { LED_DBG(LED_RED_TOGGLE); s->cnt++; DEBUG("Timer fired. Put token %d on output port. Function CB output = 0x%x.\n", s->cnt, s->output0); token_type_t *my_token = create_token(&s->cnt, sizeof(uint8_t), s->pid); if (my_token == NULL) return -ENOMEM; //SOS_CALL(s->put_token, put_token_func_t, s->output0, my_token); dispatch(s->output0, my_token); destroy_token(my_token); sys_timer_start(TIMER_TID, ((uint32_t)s->sample_rate_in_sec) * 1024L, TIMER_ONE_SHOT); break; } case MSG_FINAL: { break; } default: return -EINVAL; } return SOS_OK; }
CK_RV parse_readers(ykpiv_state *state, const CK_BYTE_PTR readers, const CK_ULONG len, ykcs11_slot_t *slots, CK_ULONG_PTR n_slots, CK_ULONG_PTR n_with_token) { CK_BYTE i; CK_BYTE_PTR p; CK_BYTE_PTR s; CK_ULONG l; slot_vendor_t slot; *n_slots = 0; *n_with_token = 0; p = readers; /* * According to pcsc-lite, the format of a reader name is: * name [interface] (serial) index slot * http://ludovicrousseau.blogspot.se/2010/05/what-is-in-pcsc-reader-name.html */ for (i = 0; i < len; i++) if (readers[i] == '\0' && i != len - 1) { slots[*n_slots].vid = get_vendor_id(p); if (slots[*n_slots].vid == UNKNOWN) { // TODO: distinguish between tokenless and unsupported? // Unknown slot, just save what info we have memset(&slots[*n_slots].info, 0, sizeof(CK_SLOT_INFO)); memset(slots[*n_slots].info.slotDescription, ' ', sizeof(slots[*n_slots].info.slotDescription)); if (strlen(p) <= sizeof(slots[*n_slots].info.slotDescription)) strncpy(slots[*n_slots].info.slotDescription, p, strlen(p)); else strncpy(slots[*n_slots].info.slotDescription, p, sizeof(slots[*n_slots].info.slotDescription)); } else { // Supported slot slot = get_slot_vendor(slots[*n_slots].vid); // Values must NOT be null terminated and ' ' padded memset(slots[*n_slots].info.slotDescription, ' ', sizeof(slots[*n_slots].info.slotDescription)); s = slots[*n_slots].info.slotDescription; l = sizeof(slots[*n_slots].info.slotDescription); strncpy((char *)s, (char*)p, l); memset(slots[*n_slots].info.manufacturerID, ' ', sizeof(slots[*n_slots].info.manufacturerID)); s = slots[*n_slots].info.manufacturerID; l = sizeof(slots[*n_slots].info.manufacturerID); if(slot.get_slot_manufacturer(s, l) != CKR_OK) goto failure; if (slot.get_slot_flags(&slots[*n_slots].info.flags) != CKR_OK) goto failure; // Treating hw and fw version the same if (slot.get_slot_version(&slots[*n_slots].info.hardwareVersion) != CKR_OK) goto failure; if (slot.get_slot_version(&slots[*n_slots].info.firmwareVersion) != CKR_OK) goto failure; if (has_token(slots + *n_slots)) { // Save token information (*n_with_token)++; if (create_token(state, p, slots + *n_slots) != CKR_OK) goto failure; } } (*n_slots)++; p += i + 1; } return CKR_OK; failure: // TODO: destroy all token objects for (i = 0; i < *n_slots; i++) if (has_token(slots + i)) destroy_token(slots + i); return CKR_FUNCTION_FAILED; }
// frees all tokens void flush_tokens(Ttoken *head) { if (head == NULL) return; if (head->next != NULL) flush_tokens((Ttoken *)head->next); destroy_token(head); }
token_stream::~token_stream () { for (auto& tok : this->toks) destroy_token (tok); }
int evaluate(struct SLList *tokens) { struct SLList cmds; init_list(&cmds); IOModifier prev_mod = NO_MODIFIER; struct ExecutableCmd *exe = (struct ExecutableCmd *)malloc(sizeof(struct ExecutableCmd)); init_executable_cmd(exe); while (tokens->length != 0) { struct Token *tok = pop_front(tokens); // check for output redirection conflicts here. if ( prev_mod == PIPE ) { if (exe->output_redir_to) { // conflict // dump the token and cmd lists. destroy_token(tok); while ( (tok = pop_front(tokens)) ) destroy_token(tok); destroy_exe_cmd(exe); destroy_cmd_list(&cmds); fprintf(stderr, "error: syntax error in output redirection.\n"); return -1; } add_back(&cmds, exe); exe = (struct ExecutableCmd *)malloc(sizeof(struct ExecutableCmd)); init_executable_cmd(exe); prev_mod = NO_MODIFIER; } // use previous token's mod, if mod exists, // and this token's info to populate redirection of exe. if (prev_mod == INPUT_REDIR) { exe->input_redir_from = make_file_path(tok->name); } else if (prev_mod == OUTPUT_REDIR) { exe->output_redir_to = make_file_path(tok->name); } else if (prev_mod == ERR_OUTPUT_REDIR) { exe->err_output_redir_to = make_file_path(tok->name); } if ( prev_mod == NO_MODIFIER ) { if ( (exe->full_path = find_cmd(tok->name)) != NULL ) { exe->args = populate_args(tok); if (tok->mod == NO_MODIFIER) { add_back(&cmds, exe); } } else { fprintf(stderr, "error: not a valid command.\n"); destroy_token(tok); destroy_exe_cmd(exe); destroy_cmd_list(&cmds); return -1; } } else if (tok->mod == NO_MODIFIER) { add_back(&cmds, exe); } prev_mod = tok->mod; destroy_token(tok); } // Execute the commands. int exe_status = execute_cmds(&cmds); // TODO: destroy the cmds list. destroy_cmd_list(&cmds); // return 0 if everything went peachy. return exe_status; }