void cmdReadoutUnprotect(void){ if(cmdGeneric(0x92)){ mdebug(10, "*** Readout Unprotect command"); wait_for_ask(); wait_for_ask(); mdebug(10, " Read Unprotect done"); } else{ /* TODO: handle nack */ handle_error(NACK_ERROR); } }
void cmdWriteUnprotect(void){ if(cmdGeneric(0x73)){ mdebug(10, "*** Write Unprotect command"); wait_for_ask(); wait_for_ask(); mdebug(10, " Write Unprotect done"); } else{ /* TODO: handle nack */ handle_error(NACK_ERROR); } }
gboolean website_update_games_database(HttpHelper* hh, const gchar* localfile, const gchar* fileurl, MudError** error) { gchar* tmpfile_templ = "mmXXXXXX"; gchar* tmpfile_name = NULL; int tmpfile = 0; int gmfile = 0; int ret = TRUE; GError* gerror = NULL; tmpfile = g_file_open_tmp (tmpfile_templ, &tmpfile_name, &gerror); if (tmpfile == -1) { g_free (tmpfile_name); *error = mud_cnv (gerror); return FALSE; } mdebug (DBG_GAMELIST, 0, "Using temp file: %s\n", tmpfile_name); ret = http_download (fileurl, tmpfile, hh); if( ret != CONNECT_OK ) { *error = mud_error_new (MUD_NETWORK_ERROR, ret, network_errmsg (ret)); ret = FALSE; } else { gmfile = open (localfile, O_WRONLY | O_CREAT | O_TRUNC, MUD_NEW_FILE_MODE); if (gmfile == -1) { *error = mud_error_new (MUD_NETWORK_ERROR, errno, strerror (errno)); close (tmpfile); ret = FALSE; } else { lseek (tmpfile, (off_t) 0, SEEK_SET); mdebug (DBG_GAMELIST, 0, "Uncompressing to %s...\n", localfile); ret = uncompress_file (tmpfile, gmfile, error); } } g_remove (tmpfile_name); // close (tmpfile); // closed in uncompress_file close (gmfile); g_free (tmpfile_name); return ret; }
void ScarabServerConnection::disconnect() { if(reader) { mdebug("Disconnecting reader %d", reader->getID()); // the call to disconnect is in the connection class after // the service is interrupted reader->setInterrupt(true); } if(writer) { mdebug("Disconnecting writer %d", writer->getID()); // the call to disconnect is in the connection class after // the service is interrupted writer->setInterrupt(true); } }
static double set_targets(double factor) { // {{{ // Set motor targets for the requested factor. If limits are exceeded, return largest acceptable factor. if (spaces[0].num_axes > 0) { // Only handle positional move if there are positional axes. double factor2 = 2 * factor - 1; // convert [0,1] to [-1,1] double alpha = factor2 * settings.alpha_max; // requested angle. // Moves are a combination of two vectors: // A is from the middle of the line from start to end, to the end. // B is from the middle of the line from start to end, to the middle of the arc. // (A and B are perpendicular.) // a and b are weights to use for A and B respectively. // a = sin(alpha)/sin(alpha_max). For small angles this breaks, so just use factor2. double denominator = sin(settings.alpha_max); double a = (denominator < 1e-10 ? factor2 : sin(alpha) / denominator); // b = cos(alpha)-cos(alpha_max)/(1-cos(alpha_max). For small angles this also breaks, so use 1-abs(factor2). double cmax = cos(settings.alpha_max); double b = (cos(alpha) - cmax) / (1 - cmax); if (std::isnan(b)) b = 1 - std::fabs(factor2); // Doesn't really matter; B == {0, 0, 0}. // Set position for xyz axes. for (int i = 0; i < 3; ++i) { if (i < spaces[0].num_axes) { spaces[0].axis[i]->settings.target = settings.P[i] + a * settings.A[i] + b * settings.B[i]; mdebug("target %d = %f P %f a %f A %f B %f amax %f factor2 %f", i, spaces[0].axis[i]->settings.target, settings.P[i],a, settings.A[i], settings.B[i], settings.alpha_max, factor2); } } } // Set all other axes with linear interpolation and compute motor positions, returning maximum allowed factor. double max_f = 1; for (int s = 0; s < NUM_SPACES; ++s) { Space &sp = spaces[s]; if (s == 2 && !settings.single) continue; for (int a = (s == 0 ? 3 : 0); a < sp.num_axes; ++a) { auto ax = sp.axis[a]; if (std::isnan(ax->settings.source)) { ax->settings.source = 0; mdebug("setting axis %d %d source from nan to 0", s, a); } ax->settings.target = ax->settings.source + factor * (ax->settings.endpos - ax->settings.source); mdebug("setting target for %d %d to %f (%f -> %f)", s, a, ax->settings.target, ax->settings.source, ax->settings.endpos); } double f = move_axes(&sp); if (max_f > f) max_f = f; } return max_f; } // }}}
int man_platform_config(man_platform_t *_platform, string config_file) { mdebug("platform=0x%x, config_file=%s\n",platform,config_file); if (!_platform) { return -1; } rtdal_machine(&machine); platform = _platform; platform->nof_nodes = 1; platform->nof_processors = machine.nof_cores; platform->ts_length_us = machine.ts_len_ns/1000; platform->core0_relative = machine.core0_relative; if (machine.scheduling == SCHEDULING_BESTEFFORT) { platform->nof_processors = 1; platform->ts_length_us = 1; } for (int i=0;i<platform->nof_processors;i++) { platform->nodes[0].processors[i].node = &platform->nodes[0]; platform->processors[i] = &platform->nodes[0].processors[i]; platform->nodes[0].processors[i].idx_in_node = i; } platform->nodes[0].id = 0; platform->nodes[0].platform = platform; return 0; }
static double move_axes(Space *s) { // {{{ bool ok = true; space_types[s->type].xyz2motors(s); // Try again if it didn't work; it should have moved target to a better location. if (!ok) { space_types[s->type].xyz2motors(s); mdebug("retried move"); } //mdebug("ok %d", ok); double factor = 1; for (int m = 0; m < s->num_motors; ++m) { //if (s->id == 0 && m == 0) //debug("check move %d %d target %f current %f", s->id, m, s->motor[m]->settings.target_pos, s->motor[m]->settings.current_pos); double distance = s->motor[m]->settings.target_pos - s->motor[m]->settings.current_pos; Motor *limit_mtr; if (settings.single) limit_mtr = s->motor[m]; else { int target = space_types[s->type].follow(s, m); if (target < 0) limit_mtr = s->motor[m]; else { int fs = target >> 8; int fa = target & 0xff; limit_mtr = spaces[fs].motor[fa]; } } check_distance(s->id, m, s->motor[m], limit_mtr, distance, settings.hwtime_step / 1e6, factor); } return factor; // */ } // }}}
/** De-allocates the interface/variables memory allocated using module_alloc * */ int module_free(module_t *module) { int i; mdebug("module_id=%d\n",module->id); if (!module) return -1; if (module->inputs) { if (pool_free(module->inputs)) return -1; module->inputs = NULL; } if (module->outputs) { if (pool_free(module->outputs)) return -1; module->outputs = NULL; } if (module->variables) { for (i=0;i<module->nof_variables;i++) { if (variable_free(&module->variables[i])) { return -1; } } if (pool_free(module->variables)) return -1; module->variables = NULL; } module->nof_inputs = 0; module->nof_outputs = 0; module->nof_variables = 0; return 0; }
void cmdReadMemory(unsigned long addr, int lng, unsigned char * data){ if(lng > data_size){ handle_error(DATA_LEN_ERROR); return; } /* TODO: handler assert error */ if(cmdGeneric(0x11)){ mdebug(10, "*** ReadMemory command"); unsigned char * addr_buffer = (unsigned char *) malloc(sizeof(unsigned char)*5); encode_addr(addr, addr_buffer); /* Get addr and crc buffer */ uart_write(addr_buffer, 5); wait_for_ask(); int n = (lng - 1) & 0xFF; unsigned char crc = n ^ 0xFF; unsigned char n_crc_buff[2]; n_crc_buff[0] = n; n_crc_buff[1] = crc; uart_write(n_crc_buff, 2); wait_for_ask(); /* read lng number of bytes */ uart_reads(data, lng); } else{ /* TODO: handle nack */ handle_error(NACK_ERROR); } }
/* Handle error */ void handle_error(error_status_type_t error_type){ char error[100]; switch(error_type){ case NACK_ERROR: strcpy(error, "nack error"); errorStatus.nack = 1; break; case TIMEOUT_ERROR: strcpy(error, "timeout error"); errorStatus.timeout = 1; break; case DATA_LEN_ERROR: strcpy(error, "data len error"); errorStatus.data_len = 1; break; case UNKNOWN_ERROR: strcpy(error, "unknown error"); errorStatus.unknown = 1; break; default: strcpy(error, "unknown error"); errorStatus.unknown = 1; break; } mdebug(0, error); }
void signal_cb(evutil_socket_t fd, short what, void *arg) { mdebug("signal_cb"); struct event_base *base = (struct event_base *)arg; event_base_loopbreak(base); dispatch_conn_new(-1, 'k', NULL); }
static int mapping_alloc(mapping_t *m, int nof_modules, int nof_processors) { int i; mdebug("addr=0x%x, nof_modules=%d\n",m,nof_modules); if (m->p_res) return -1; m->p_res = (int*) pool_alloc(nof_modules,sizeof(int)); if (!m->p_res) return -1; memset(m->modules_x_node,0,sizeof(int)*MAX(nodes)); join_function = calloc(1, sizeof (int) * nof_modules); joined_function = calloc(1, sizeof (int) * nof_modules); joined_function_inv = calloc(1, sizeof (int) * nof_modules); tmp_stages = calloc(1, sizeof (int) * nof_modules); tmp_c = calloc(1, sizeof (float) * nof_modules); wave.c = calloc(1, sizeof (float) * nof_modules); wave.force = calloc(1, sizeof (int) * nof_modules); wave.b = calloc(1, sizeof (float*) * nof_modules); for (i = 0; i < nof_modules; i++) { wave.b[i] = calloc(1, sizeof (float) * nof_modules); } tmp_b = calloc(1, sizeof (float*) * nof_modules); for (i = 0; i < nof_modules; i++) { tmp_b[i] = calloc(1, sizeof (float) * nof_modules); } plat.C = calloc(1, sizeof (int) * nof_processors); plat.B = calloc(1, sizeof (float*) * nof_processors); for (i = 0; i < nof_processors; i++) { plat.B[i] = calloc(1, sizeof (float) * nof_processors); } result.P_m = m->p_res; return 0; }
static int mapping_free(mapping_t *m, int nof_modules, int nof_processors) { mdebug("addr=0x%x\n",m); int i; free(join_function); free(joined_function); free(joined_function_inv); free(tmp_stages); free(tmp_c); free(wave.c); free(wave.force); for (i=0;i<nof_modules;i++) { free(wave.b[i]); } free(wave.b); for (i=0;i<nof_modules;i++) { free(tmp_b[i]); } free(tmp_b); free(plat.C); for (i=0;i<nof_processors;i++) { free(plat.B[i]); } free(plat.B); if (m->p_res) { if (pool_free(m->p_res)) { return -1; } m->p_res = NULL; } return 0; }
void client_disconnect_cb(conn *c) { mdebug("client_disconnect_cb"); if (c->user) { user_mgr->del_user((user_t *)c->user); } }
void client_connect_cb(conn *c, int ok) { mdebug("client_connect_cb ok:%d", ok); if (0 == ok) return; do { struct timeout_info *ti = (struct timeout_info *)malloc(sizeof(struct timeout_info)); if (NULL == ti) break; ti->c = c; ti->timer = evtimer_new(c->thread->base, expire_timer_cb, ti); if (NULL == ti->timer) { free(ti); break; } struct timeval tv = {5, 0}; if (0 > evtimer_add(ti->timer, &tv)) { free(ti->timer); free(ti); break; } return; } while (0); disconnect(c); }
/* Get Version of the ST Device */ unsigned char cmdGetVersion(void){ if(cmdGeneric(0x01)){ mdebug(10, "*** GetVersion command"); unsigned char version = uart_read(); unsigned char * buff = (unsigned char *) malloc(sizeof(unsigned char) * 2); uart_reads(buff, 2); wait_for_ask(); mdebug(10, " Bootloader version"); return version; } else{ /* TODO: Handle nack ? */ handle_error(NACK_ERROR); return 0; } }
int initChip(void){ /* Set boot */ uart_set_rts(0); reset(); unsigned char cmd[] = {0x7F}; uart_write(cmd, 1); /* tell bootloader to startup */ time_sleep(0.1); //need this for some reason int ok = wait_for_ask(); if(ok){ mdebug(0, "init success"); } else{ mdebug(0, "init failed"); } return ok; }
/** \brief Allocates memory for modules in a waveform. Does NOT call module_alloc() individually. * \param nof_modules Number of modules that will be allocated */ int waveform_alloc(waveform_t *waveform, int nof_modules) { mdebug("waveform_id=%d, nof_modules=%d\n",waveform->id, nof_modules); if (!waveform) return -1; if (waveform->modules) return -1; waveform->modules = (module_t*) pool_alloc(nof_modules, sizeof(module_t)); if (!waveform->modules) return -1; waveform->nof_modules = nof_modules; return 0; }
static void expire_timer_cb(int fd, short what, void *arg) { return; mdebug("expire_timer_cb"); struct timeout_info *ti = (struct timeout_info *)arg; disconnect(ti->c); free(ti->timer); free(ti); }
/** Returns a pointer to the first module with id the second parameter * \returns a non-null pointer if found or null otherwise */ module_t* waveform_find_module_id(waveform_t *w, int module_id) { mdebug("waveform=%s, module_id=%d\n",w->name, module_id); int i=0; while(i<w->nof_modules && w->modules[i].id != module_id) i++; if (i==w->nof_modules) { return NULL; } return &w->modules[i]; }
/** Returns a pointer to the first module with name the second parameter * \returns a non-null pointer if found or null otherwise */ module_t* waveform_find_module_name(waveform_t *w, char *name) { mdebug("waveform=%s, obj_name=%s\n",w->name, name); int i=0; while(i<w->nof_modules && strcmp(w->modules[i].name, name)) i++; if (i==w->nof_modules) { return NULL; } return &w->modules[i]; }
/** De-allocates the memory allocated using waveform_alloc(). Does NOT call module_free() * individually for each module */ int waveform_free(waveform_t *waveform){ mdebug("waveform_id=%d, nof_modules=%d\n",waveform->id, waveform->nof_modules); if (!waveform) return -1; if (waveform->modules) { if (pool_free(waveform->modules)) return -1; waveform->modules = NULL; } waveform->nof_modules = 0; return 0; }
NetworkReturn * ScarabServer::startListening() { NetworkReturn * rc = new NetworkReturn(); mdebug("startListening()"); if(listening) { rc->setMWorksCode(NR_SUCCESS_NETWORK_MESSAGE); rc->setInformation("Server is already running."); return rc; } if(listenUri.size() == 0) { rc->setMWorksCode(NR_FAILED); rc->setInformation("URI not specified for server"); return rc; } std::string fullUri = createScarabURI(); if(fullUri.size() == 0) { rc->setMWorksCode(NR_FAILED); rc->setInformation("Could not create a valid URI"); return rc; } int error = 1; // mnetwork("Trying listening socket at %s on port %d", // listenAddress.c_str(), // listenPort); // mprintf("URI = %s", fullUri.c_str()); listeningSocket = scarab_session_listen(fullUri.c_str()); error = getScarabError(listeningSocket); while(error) { mnetwork("Failed to open a listening socket at %s on port %d", listenAddress.c_str(), listenPort); //mdebug("Maybe print out why here??"); // if there is another available port we will try again if(!chooseNewPort()) { rc->setMWorksCode(NR_FATAL_ERROR); rc->setPackageCode(error); rc->setOSErrorCode(getScarabOSError(listeningSocket)); rc->setInformation("Ran out of ports to listen on. Networking impossible"); rc->appendInformation(getScarabErrorDescription(error)); rc->appendInformation(getOSErrorDescription( getScarabOSError(listeningSocket))); return rc; } fullUri = createScarabURI(); listeningSocket = scarab_session_listen(fullUri.c_str()); error = getScarabError(listeningSocket); } mnetwork("Listening socket started at %s on port %d", listenAddress.c_str(), listenPort); listening = true; return rc; }
/** \brief Deallocates the memory of a variable allocated using variable_alloc() * */ int variable_free(variable_t *variable) { mdebug("variable_id=%d, nof_modes=%d, size=%d\n",variable->id, variable->nof_modes, variable->size); int i; if (!variable) return -1; for (i=0;i<variable->nof_modes;i++) { if (!variable->init_value[i]) return -1; if (pool_free(variable->init_value[i])) return -1; } return 0; }
void abort_move(int pos) { // {{{ aborting = true; //debug("abort pos %d", pos); //debug("abort; cf %d rf %d first %d computing_move %d fragments, regenerating %d ticks", current_fragment, running_fragment, first_fragment, computing_move, pos); //debug("try aborting move"); current_fragment = running_fragment; //debug("current_fragment = running_fragment; %d", current_fragment); //debug("current abort -> %x", current_fragment); while (pos < 0) { if (current_fragment == first_fragment) { pos = 0; } else { current_fragment = (current_fragment + FRAGMENTS_PER_BUFFER - 1) % FRAGMENTS_PER_BUFFER; //debug("current_fragment = (current_fragment + FRAGMENTS_PER_BUFFER - 1) %% FRAGMENTS_PER_BUFFER; %d", current_fragment); pos += SAMPLES_PER_FRAGMENT; running_fragment = current_fragment; } } restore_settings(); //debug("free abort reset"); current_fragment_pos = 0; computing_move = true; while (computing_move && current_fragment_pos < unsigned(pos)) { //debug("abort reconstruct %d %d", current_fragment_pos, pos); apply_tick(); } if (spaces[0].num_axes > 0) cpdebug(0, 0, "ending hwpos %f", arch_round_pos(0, 0, spaces[0].motor[0]->settings.current_pos) + avr_pos_offset[0]); // Flush queue. settings.queue_start = 0; settings.queue_end = 0; settings.queue_full = false; // Copy settings back to previous fragment. store_settings(); computing_move = false; current_fragment_pos = 0; for (int s = 0; s < NUM_SPACES; ++s) { Space &sp = spaces[s]; sp.settings.dist[0] = 0; sp.settings.dist[1] = 0; for (int a = 0; a < sp.num_axes; ++a) { //debug("setting axis %d source to %f", a, sp.axis[a]->settings.current); if (!std::isnan(sp.axis[a]->settings.current)) sp.axis[a]->settings.source = sp.axis[a]->settings.current; sp.axis[a]->settings.dist[0] = NAN; sp.axis[a]->settings.dist[1] = NAN; } } mdebug("aborted move"); aborting = false; } // }}}
void cmdEraseMemory(unsigned char * sectors, int secLen){ if(extendedErase){ mdebug(0, "Cmd extended erase memory"); cmdExtendedEraseMemory(); return; } if(cmdGeneric(0x43)){ mdebug(10, "*** Erase memory command"); if(sectors == NULL){ /* Global erase */ unsigned char cmd[] = {0xFF}; uart_write(cmd, 1); cmd[0] = 0x00; uart_write(cmd, 1); } else{ /* Sectors erase */ unsigned char cmd[1]; cmd[0] = secLen -1; //(strlen((char *)sectors)-1) & 0xFF; uart_write(cmd, 1); unsigned char crc[] = {0xFF}; unsigned char c[1]; for(int i = 0; i<secLen; i++){ c[0] = sectors[i]; crc[0] = crc[0] ^ c[0]; uart_write(c, 1); } uart_write(crc, 1); wait_for_ask(); mdebug(10, " Erase memory done"); } } else{ /* TODO: handle nack */ mdebug(10, "*** Erase memory command failed"); handle_error(NACK_ERROR); } }
void cmdGo(unsigned long addr){ if(cmdGeneric(0x21)){ mdebug(10, "*** Go command"); unsigned char * addr_buffer = (unsigned char *) malloc(sizeof(unsigned char)*5); encode_addr(addr, addr_buffer); /* Get addr and crc buffer */ uart_write(addr_buffer, 5); wait_for_ask(); } else{ /* TODO: handle nack */ handle_error(NACK_ERROR); } }
void cmdExtendedEraseMemory(void){ if(cmdGeneric(0x44)){ mdebug(10, "*** Extended Erase memory command"); /* Global mass erase */ unsigned char cmd[] = {0xFF}; uart_write(cmd, 1); uart_write(cmd, 1); /* Checksum */ cmd[0] = 0x00; uart_write(cmd, 1); int tmp = get_uart_timeout(); //set_uart_timeout(30); time_sleep(10); //can take 30 sec /* Extended erase (0x44), can take ten seconds or more */ wait_for_ask(); set_uart_timeout(tmp); /* set back to default */ mdebug(10, " Extended Erase memory done"); } else{ /* TODO: handle nack */ handle_error(NACK_ERROR); } }
int writeMemory(unsigned long addr){ /* TODO: write all data of image from flash to stm32 */ /* return 1 if successful and 0 if not */ int offs = 0; unsigned char * data = (unsigned char *)malloc(sizeof(unsigned char)*data_size); int complete = 0; while(!complete){ mdebug(0, "@"); //informs python server to send data bytes debug_read(data,data_size); //mdebug(0, "Got data"); if(data[0] == 's' && data[1] == 't' && data[2] == 'o' && data[3] == 'p' && data[4] == '!' && data[5] == '!'){ //complete! return 0; } //mdebug(5, "Write bytes"); cmdWriteMemory(addr, data, data_size); addr = addr + data_size; } mdebug(0, "Write Complete"); return 0; }
/** Allocates memory for nof_modes initialization values in a variable. For each mode, * allocates variable->size bytes * */ int variable_alloc(variable_t *variable, int nof_modes) { mdebug("variable_id=%d, nof_modes=%d, size=%d\n",variable->id, nof_modes, variable->size); int i; if (!variable) return -1; for (i=0;i<nof_modes;i++) { if (variable->init_value[i]) { aerror("already initialized???\n"); } variable->init_value[i] = pool_alloc(1,(size_t) variable->size); if (!variable->init_value[i]) return -1; } variable->nof_modes = nof_modes; return 0; }