ParserStatus parse_frame(in_cmd_t *cmd, plchar_t *payload, const frame_t frame) { int in_pos = 0; int in_len = strlen(frame); if (frame[in_pos++] != START_CHAR) { return PARSER_INVALID_START; } mk_address_t addr = addr_decode(frame[in_pos++]); if (addr < 1 || addr > 3) { return PARSER_INVALID_ADDRESS; } in_cmd_t _cmd = MERGE_ADDR_CMD(addr, frame[in_pos++]); if (!in_command_exists(_cmd)) { return PARSER_INVALID_COMMAND; } const char *crc_chars = &frame[in_len - 3]; crc_t crc = calc_crc(frame, in_len - 3); if (!crc_ok(crc, crc_chars)) { return PARSER_INVALID_CRC; /* invalid crc */ } (void)mb64_decode(payload, in_len - 6, &frame[in_pos]); *cmd = _cmd; return PARSER_NO_ERROR; }
boolean s_event::isValid() { boolean blank = true; uint8_t * p = (uint8_t*)this; for(uint8_t k =0; k < sizeof(s_event); k++) { if(p[k] != 0xFF) { blank = false; break; } } if(blank) return 0; if(calc_crc((uint8_t*)this, sizeof(s_event))) { Serial.println("s_event: INVALID CRC"); return 0; } return 1; }
bool Write(const void *data, size_t length, unsigned timeout_ms=5000) { if (!port.FullWrite(data, length, timeout_ms)) return false; crc = calc_crc((const uint8_t *)data, length, crc); return true; }
s_event::s_event(long timeStamp, uint8_t port, uint8_t state) { this->timeStamp = timeStamp; this->port = port; this->state = state; ack = 0xFF; crc = calc_crc((uint8_t*)this, sizeof(s_event) - 1); }
uint32_t Protocol::application_state_checksum(uint32_t (*calc_crc)(const uint8_t* data, uint32_t len), uint32_t subscriptions_crc, uint32_t describe_app_crc, uint32_t describe_system_crc) { uint32_t chk[3]; chk[0] = subscriptions_crc; chk[1] = describe_app_crc; chk[2] = describe_system_crc; return calc_crc((uint8_t*)chk, sizeof(chk)); }
void cfg_save() { global_addl_config.crc = 0 ; global_addl_config.length = sizeof(addl_config_t); global_addl_config.crc = calc_crc(&global_addl_config,sizeof(addl_config_t)); cfg_write_struct( &global_addl_config ); }
bool LX::SendPacket(Port &port, Command command, const void *data, size_t length, OperationEnvironment &env, unsigned timeout_ms) { return SendCommand(port, command) && port.FullWrite(data, length, env, timeout_ms) && port.Write(calc_crc(data, length, 0xff)); }
/* =========================================================== trimite un mesaj de tip ack pentru primul pachet trimis de sender(include dimensiunea ferestrei aleasa ca minimul dintre parametrul window si cel trimis de sender intial) =========================================================== */ void send_ack2(unsigned short seq_nr, unsigned short int *tbl_crc, unsigned short int window) { msg t; t.type = ACK; memcpy(t.payload + 2, &seq_nr, 2); memcpy(t.payload + 4, &window, 2); unsigned short int crc = calc_crc(&t, tbl_crc); memcpy(t.payload, &crc, 2); send_message(&t); }
bool LX::SendPacket(Port &port, Command command, const void *data, size_t length, OperationEnvironment &env, std::chrono::steady_clock::duration timeout) { return SendCommand(port, command) && port.FullWrite(data, length, env, timeout) && port.Write(calc_crc(data, length, 0xff)); }
bool LX::ReadCRC(Port &port, void *buffer, size_t length, OperationEnvironment &env, unsigned timeout_ms) { uint8_t crc; return port.FullRead(buffer, length, env, timeout_ms) && port.FullRead(&crc, sizeof(crc), env, timeout_ms) && calc_crc(buffer, length, 0xff) == crc; }
// parse received packet void ORCPClass::parse_packet(void) { if(input_buffer_size >= CP_MIN_LENGTH && input_buffer[0] == identity_id) { // check CRC uint8_t crc = input_buffer[input_buffer_size-CP_PACKET_CRC_LENGTH]; uint8_t mes_crc = calc_crc(input_buffer, input_buffer_size-CP_PACKET_CRC_LENGTH); if(crc != mes_crc) goto end_parse; header.IT = input_buffer[1]; // System Code header.SC = input_buffer[2]; if(header.SC == CP_DUINO_BS) { header.SS = input_buffer[3]; switch(header.SS) // Base Subsystem Codes { case CP_DUINO_PINMODE: if(currentPinModeCallback) (*currentPinModeCallback)(input_buffer[4], input_buffer[5]); break; case CP_DUINO_DIGITALREAD: if(currentDigitalReadCallback) (*currentDigitalReadCallback)(input_buffer[4], 0); break; case CP_DUINO_ANALOGREAD: if(currentAnalogReadCallback) (*currentAnalogReadCallback)(input_buffer[4], 0); break; case CP_DUINO_DIGITALWRITE: if(currentDigitalWriteCallback) (*currentDigitalWriteCallback)(input_buffer[4], input_buffer[5]); break; case CP_DUINO_ANALOGWRITE: if(currentAnalogWriteCallback) (*currentAnalogWriteCallback)(input_buffer[4], (input_buffer[5]<<8 | input_buffer[6]) ); break; case CP_DUINO_SENDSTRING: if(currentStringCallback) { input_buffer[input_buffer_size-2] = 0; (*currentStringCallback)((char*)(input_buffer+CP_PACKET_HEADER_LENGTH)); } break; } } } end_parse: // go ready to get next packet input_buffer_size = 0; esc_flag = false; }
void cfg_load() { memset( &global_addl_config, 0, sizeof(addl_config_t) ); addl_config_t tmp ; cfg_read_struct( &tmp ); // the config in flash is bigger than mine. if( tmp.length > sizeof(addl_config_t) ) { // we cannot crc what we have not read. (for now). LOGB("cfg oversized\n"); return ; } if( calc_crc(&tmp,tmp.length) != 0 ) { // corrupted. LOGB("cfg crc fail\n"); return ; } // copy the smaller config into our bigger config. // leaving the rest 0. memcpy(&global_addl_config,&tmp,tmp.length); // range limit R(global_addl_config.userscsv,1); R(global_addl_config.micbargraph,1); R(global_addl_config.debug,1); R(global_addl_config.rbeep,1); R(global_addl_config.promtg,1); R(global_addl_config.boot_demo,1); // R(global_addl_config.boot_splash,0); // unused R(global_addl_config.netmon,3); R(global_addl_config.datef,5); // restore dmrid if( ( global_addl_config.cp_override & CPO_DMR ) == CPO_DMR ) { md380_radio_config.dmrid = global_addl_config.dmrid ; } // // restore radio name // if (global_addl_config.userscsv == 1) { // cfg_fix_radioname(); // } // global_addl_config.experimental is intentionally not permanent global_addl_config.experimental = 0; #if defined(FW_D13_020) || defined(FW_S13_020) #else global_addl_config.netmon = 0 ; #endif }
FileHeader *get_file_header( FILE *in ) /* ** Creates a FileHeader structure and fills it with data beginning at the ** current file location in 'in'. Obviously, it's best if you've already ** advanced to where a FileHeader is stored in the file. If an error ** occurs, NULL is returned. */ { FileHeader *fileHdr; if( fileHdr = (FileHeader *)malloc( FileHdrSize ) ) { if( FileHdrSize == fread( (void *)fileHdr, sizeof( ubyte ), FileHdrSize, in ) || ask( "Read error on file header" ) ) { CheckSum crc; crc = calc_crc( (ubyte *)fileHdr, FileHdrSize - sizeof( CheckSum ), INIT_CRC ); #ifdef SWAP /* We MUST calculate the checksum BEFORE we swab the data! */ swap_long( (ulong *)&fileHdr->fType ); swap_long( (ulong *)&fileHdr->fCreator ); swap_word( (uword *)&fileHdr->FndrFlags ); swap_long( &fileHdr->creationDate ); swap_long( &fileHdr->modDate ); swap_long( &fileHdr->rsrcLength ); swap_long( &fileHdr->dataLength ); swap_long( &fileHdr->compRLength ); swap_long( &fileHdr->compDLength ); swap_word( &fileHdr->rsrcCRC ); swap_word( &fileHdr->dataCRC ); swap_word( &fileHdr->hdrCRC ); #endif if( crc == fileHdr->hdrCRC || ask( "Bad checksum on file header" ) ) { ubyte len = (ubyte)*(fileHdr->fName); /* Convert the Pascal-style name string to C-style */ memmove( fileHdr->fName, fileHdr->fName + 1, len ); *(fileHdr->fName + len) = '\0'; return( fileHdr ); } } free( fileHdr ); } else clean_exit( "Out of memory!" ); return( NULL ); }
static void update_crc (char *ip) { int n = calc_crc ((unsigned char *) (ip + 0x40), 16); char buf[5]; sprintf (buf, "%04X", n); if (memcmp (buf, ip + 0x20, 4)) { printf ("Setting CRC to %s (was %.4s)\n", buf, ip + 0x20); memcpy (ip + 0x20, buf, 4); } }
/* Caller may need to use label_get_handler to create label struct! */ int label_write(struct device *dev, struct label *label) { #pragma pack(8) char buf[LABEL_SIZE]; //__attribute((aligned(8))); #pragma pack() struct label_header *lh = (struct label_header *) buf; int r = 1; if (!label->labeller->ops->write) { log_err("Label handler does not support label writes"); return 0; } if ((LABEL_SIZE + (label->sector << SECTOR_SHIFT)) > LABEL_SCAN_SIZE) { log_error("Label sector %" PRIu64 " beyond range (%ld)", label->sector, LABEL_SCAN_SECTORS); return 0; } memset(buf, 0, LABEL_SIZE); strncpy((char *)lh->id, LABEL_ID, sizeof(lh->id)); lh->sector_xl = xlate64(label->sector); lh->offset_xl = xlate32(sizeof(*lh)); if (!(label->labeller->ops->write)(label, buf)) { stack; return 0; } lh->crc_xl = xlate32(calc_crc(INITIAL_CRC, &lh->offset_xl, LABEL_SIZE - ((char *) &lh->offset_xl - (char *) lh))); //((void *) &lh->offset_xl - (void *) lh))); if (!dev_open(dev)) { stack; return 0; } log_info("%s: Writing label to sector %" PRIu64, dev_name(dev), label->sector); if (!dev_write(dev, label->sector << SECTOR_SHIFT, LABEL_SIZE, buf)) { log_debug("Failed to write label to %s", dev_name(dev)); r = 0; } if (!dev_close(dev)) stack; return r; }
*/msg message(unsigned short int seq, char * type) { msg m; if(strcmp(type, "ACK") == 0) m.type = 3; else m.type = 4; m.len = strlen(m.payload+4)+1; sprintf(m.payload+4,type); memcpy(m.payload+2,&seq,2); unsigned short int crc = calc_crc(&m,crc_table); memcpy(m.payload,&crc,2); return m; }
DSMVOID bkioRemoveLkFile ( dsmContext_t *pcontext, TEXT *dbname) /* name of database */ { TEXT namebuf[MAXPATHN+1]; LONG retMakePath = 0; int lockret; UCOUNT nameCrc; int i; dbcontext_t *pdbcontext = pcontext->pdbcontext; /* delete the dbname.lk file */ if(pdbcontext->pdatadir) utmypath(namebuf,pdbcontext->pdatadir,dbname, (TEXT *)".lk"); else retMakePath = utMakePathname(namebuf, sizeof(namebuf), dbname, (TEXT *)".lk"); if (retMakePath != DBUT_S_SUCCESS) { /* Report error */ dbUserError(pcontext, retMakePath, errno, 0, (DSMVOID *)NULL, (DSMVOID *)NULL); } nameCrc = calc_crc((UCOUNT)0, namebuf, (COUNT)stlen(namebuf)); for (i = 0; i < 240; i++) { if ((lkstr[i].nameCrc == nameCrc) && (lkstr[i].lkHandle != (fileHandle_t)0) && (lkstr[i].lkHandle != INVALID_FILE_HANDLE) ) { /* we found the handle */ lockret = utUnlockLkFile(&lkstr[i].lkHandle); lkstr[i].nameCrc = 0; break; } } #if OPSYS==UNIX unlink((const char *)namebuf); #endif #if OPSYS==WIN32API lockret = DeleteFile(namebuf); #endif }
void radio_tx_data(unsigned char data) { unsigned char cnt; calc_crc(&radio_tx_crc, data); for (cnt = 0; cnt < 8; cnt++) { RADIO_SND_LAT |= (1 << RADIO_SND_BIT); if (data & 1) { __delay_us(300); } else { __delay_us(200); } RADIO_SND_LAT &= ~(1 << RADIO_SND_BIT); __delay_us(100); data >>= 1; } }
int build_frame(frame_t dest, const out_cmd_t cmd, const plchar_t *payload, const size_t input_len) { int pos = 0; /* add start-of-frame, address and command: */ dest[pos++] = START_CHAR; dest[pos++] = addr_encode(GET_ADDR(cmd)); dest[pos++] = GET_CMD_CHAR(cmd); /* add and encode payload: */ pos += mb64_encode(&dest[pos], payload, input_len); /* add CRC and end-of-frame character: */ pos += calc_crc_chars(&dest[pos], calc_crc(dest, pos)); dest[pos++] = END_CHAR; dest[pos] = '\0'; return pos; }
bool LX::ReadCRC(Port &port, void *buffer, size_t length, OperationEnvironment &env, std::chrono::steady_clock::duration first_timeout, std::chrono::steady_clock::duration subsequent_timeout, std::chrono::steady_clock::duration total_timeout) { uint8_t crc; return port.FullRead(buffer, length, env, first_timeout, subsequent_timeout, total_timeout) && port.FullRead(&crc, sizeof(crc), env, subsequent_timeout, subsequent_timeout, subsequent_timeout) && calc_crc(buffer, length, 0xff) == crc; }
int test_crc24a() { char input_byte[2] = {0xF2, 0x3B}; char output_ref[5] = {0xF2, 0x3B, 0xF8, 0x23, 0x56}; char io_bit[40]; char output_byte[2]; int i; unpack(input_byte, 2, io_bit); calc_crc(io_bit, 16, CRC_POLY_24A); pack(io_bit, 40, output_byte); for (i=0; i<2; i++) { if (output_byte[i] != output_ref[i]) return -1; } return 0; }
int write_sector (int fd, unsigned int sector){ char buffer[BUFFER_SIZE], ans[BUFFER_SIZE]; unsigned int adr; if (sector >= 7) { fprintf(stderr,"ERROR: Writing sector %d is forbidden\n", sector); return; }; printf("Erasing secor %d \n", sector); sprintf(buffer, ":0300000401%04x", sector * 1024); checksum(buffer); if (0 != send_string(fd, buffer, ans)) { return(-20); }; for (adr = sector * 1024; adr < (sector * 1024 + 1023); adr += 64){ make_ihex(adr, buffer); if (0 != send_string(fd, buffer, ans)) return (20); }; sprintf(buffer, ":01000005%02X", sector * 4); checksum(buffer); if (0 != send_string(fd, buffer, ans)) { return(20); ; }; sprintf(buffer,"%08X",calc_crc(sector * 1024)); if (0 != strncmp(ans, buffer, 8)){ printf("Calculated CRC Sector %d = %s\n", sector, buffer); printf("Read CRC = %s\n", ans); return(20); } else printf("Sector %d CRC OK.\n", sector); return(0); };
TEST(util, crc) { ASSERT_EQ(4, efiRound(4.4, 1)); ASSERT_FLOAT_EQ(1.2, efiRound(1.2345, 0.1)); print("*************************************** testCrc\r\n"); const char * A = "A"; ASSERT_EQ( 168, calc_crc((const crc_t *) A, 1)) << "crc8"; uint32_t c = crc32(A, 1); printf("crc32(A)=%x\r\n", c); assertEqualsM("crc32 1", 0xd3d99e8b, c); const char * line = "AbcDEFGF"; c = crc32(line, 8); printf("crc32(line)=%x\r\n", c); assertEqualsM("crc32 line", 0x4775a7b1, c); c = crc32(line, 1); c = crc32inc(line + 1, c, 8 - 1); assertEqualsM("crc32 line inc", 0x4775a7b1, c); }
/* send 1 byte and do the byte stuffing Frsky stuff This can send more than 1 byte eventually */ void AP_Frsky_Telem::frsky_send_byte(uint8_t value) { if (_protocol == FrSkyDPORT) { const uint8_t x5E[] = { 0x5D, 0x3E }; const uint8_t x5D[] = { 0x5D, 0x3D }; switch (value) { case 0x5E: _port->write( x5E, sizeof(x5E)); break; case 0x5D: _port->write( x5D, sizeof(x5D)); break; default: _port->write(&value, sizeof(value)); break; } } else { //SPORT calc_crc(value); const uint8_t x7E[] = { 0x7D, 0x5E }; const uint8_t x7D[] = { 0x7D, 0x5D }; switch (value) { case 0x7E: _port->write( x7E, sizeof(x7E)); break; case 0x7D: _port->write( x7D, sizeof(x7D)); break; default: _port->write(&value, sizeof(value)); break; } } }
/** **************************************************************************************** * @brief Transmit a boot image to an SPI slave device **************************************************************************************** */ void spi_send_image (void) { uint8_t mode; uint8_t crc; uint8_t header_ack; uint8_t payload_ack; spi_booter_pad.pin = SPI_CS_PIN; spi_booter_pad.port = SPI_GPIO_PORT; crc = calc_crc(SPI_LENGTH); mode = 0x00; spi_init(&spi_booter_pad, SPI_MODE_8BIT, SPI_ROLE_MASTER, SPI_CLK_IDLE_POL_LOW, SPI_PHA_MODE_0, SPI_MINT_DISABLE, SPI_XTAL_DIV_4); do{ header_ack = send_header(SPI_LENGTH,crc,mode); if (header_ack) { payload_ack = send_payload(SPI_8BIT,SPI_LENGTH); break; } } while((header_ack&payload_ack)!=1); spi_release(); }
int main(void) { uint16_t crc; int i; // Sample APRS frame payload - FCS = {0x76, 0x4A} uint8_t testvector[] = { 0x82, 0xA0, 0xB4, 0x60, 0x60, 0x60, 0xE0, // "APZ___" 0x9C, 0x60, 0x86, 0x82, 0x98, 0x98, 0xE3, // "N0CALL-1" 0x03, 0xF0, 0x2C, 0x41}; // Control PID ",A" size_t testlength = sizeof(testvector); // Calculate the FCS crc = calc_crc(testvector, testlength); // "Transmit" the complete frame printf(" ........7E\n"); for (i=0; i<testlength; i++) { send_octet(testvector[i]); } send_octet(crc & 0xFF); // Send FCS bits 8-15 send_octet((crc >> 8) & 0xFF); // Send FCS bits 0-7 printf("7E........\n"); return 0; }
crc flashStateCrc(FlashState *state) { return calc_crc((const crc*) &state->persistentConfiguration, sizeof(persistent_config_s)); }
int callerid_feed_jp(struct callerid_state *cid, unsigned char *ubuf, int len, format_t codec) { int mylen = len; int olen; int b = 'X'; int b2; int res; int x; short *buf; buf = ast_alloca(2 * len + cid->oldlen); memcpy(buf, cid->oldstuff, cid->oldlen); mylen += cid->oldlen / 2; for (x = 0; x < len; x++) buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]); while (mylen >= 160) { b = b2 = 0; olen = mylen; res = fsk_serial(&cid->fskd, buf, &mylen, &b); if (mylen < 0) { ast_log(LOG_ERROR, "No start bit found in fsk data.\n"); return -1; } buf += (olen - mylen); if (res < 0) { ast_log(LOG_NOTICE, "fsk_serial failed\n"); return -1; } if (res == 1) { b2 = b; b &= 0x7f; /* crc checksum calculation */ if (cid->sawflag > 1) cid->crc = calc_crc(cid->crc, (unsigned char) b2); /* Ignore invalid bytes */ if (b > 0xff) continue; /* skip DLE if needed */ if (cid->sawflag > 0) { if (cid->sawflag != 5 && cid->skipflag == 0 && b == 0x10) { cid->skipflag = 1 ; continue ; } } if (cid->skipflag == 1) cid->skipflag = 0 ; /* caller id retrieval */ switch (cid->sawflag) { case 0: /* DLE */ if (b == 0x10) { cid->sawflag = 1; cid->skipflag = 0; cid->crc = 0; } break; case 1: /* SOH */ if (b == 0x01) cid->sawflag = 2; break ; case 2: /* HEADER */ if (b == 0x07) cid->sawflag = 3; break; case 3: /* STX */ if (b == 0x02) cid->sawflag = 4; break; case 4: /* SERVICE TYPE */ if (b == 0x40) cid->sawflag = 5; break; case 5: /* Frame Length */ cid->sawflag = 6; break; case 6: /* NUMBER TYPE */ cid->sawflag = 7; cid->pos = 0; cid->rawdata[cid->pos++] = b; break; case 7: /* NUMBER LENGTH */ cid->sawflag = 8; cid->len = b; if ((cid->len+2) >= sizeof(cid->rawdata)) { ast_log(LOG_WARNING, "too long caller id string\n") ; return -1; } cid->rawdata[cid->pos++] = b; break; case 8: /* Retrieve message */ cid->rawdata[cid->pos++] = b; cid->len--; if (cid->len<=0) { cid->rawdata[cid->pos] = '\0'; cid->sawflag = 9; } break; case 9: /* ETX */ cid->sawflag = 10; break; case 10: /* CRC Checksum 1 */ cid->sawflag = 11; break; case 11: /* CRC Checksum 2 */ cid->sawflag = 12; if (cid->crc != 0) { ast_log(LOG_WARNING, "crc checksum error\n") ; return -1; } /* extract caller id data */ for (x = 0; x < cid->pos;) { switch (cid->rawdata[x++]) { case 0x02: /* caller id number */ cid->number[0] = '\0'; cid->name[0] = '\0'; cid->flags = 0; res = cid->rawdata[x++]; ast_copy_string(cid->number, &cid->rawdata[x], res+1); x += res; break; case 0x21: /* additional information */ /* length */ x++; /* number type */ switch (cid->rawdata[x]) { case 0x00: /* unknown */ case 0x01: /* international number */ case 0x02: /* domestic number */ case 0x03: /* network */ case 0x04: /* local call */ case 0x06: /* short dial number */ case 0x07: /* reserved */ default: /* reserved */ ast_debug(2, "cid info:#1=%X\n", (unsigned)cid->rawdata[x]); break ; } x++; /* numbering plan octed 4 */ x++; /* numbering plan octed 5 */ switch (cid->rawdata[x]) { case 0x00: /* unknown */ case 0x01: /* recommendation E.164 ISDN */ case 0x03: /* recommendation X.121 */ case 0x04: /* telex dial plan */ case 0x08: /* domestic dial plan */ case 0x09: /* private dial plan */ case 0x05: /* reserved */ default: /* reserved */ ast_debug(2, "cid info:#2=%X\n", (unsigned)cid->rawdata[x]); break ; } x++; break ; case 0x04: /* no callerid reason */ /* length */ x++; /* no callerid reason code */ switch (cid->rawdata[x]) { case 'P': /* caller id denied by user */ case 'O': /* service not available */ case 'C': /* pay phone */ case 'S': /* service congested */ cid->flags |= CID_UNKNOWN_NUMBER; ast_debug(2, "no cid reason:%c\n", cid->rawdata[x]); break ; } x++; break ; case 0x09: /* dialed number */ /* length */ res = cid->rawdata[x++]; /* dialed number */ x += res; break ; case 0x22: /* dialed number additional information */ /* length */ x++; /* number type */ switch (cid->rawdata[x]) { case 0x00: /* unknown */ case 0x01: /* international number */ case 0x02: /* domestic number */ case 0x03: /* network */ case 0x04: /* local call */ case 0x06: /* short dial number */ case 0x07: /* reserved */ default: /* reserved */ if (option_debug > 1) ast_log(LOG_NOTICE, "did info:#1=%X\n", (unsigned)cid->rawdata[x]); break ; } x++; /* numbering plan octed 4 */ x++; /* numbering plan octed 5 */ switch (cid->rawdata[x]) { case 0x00: /* unknown */ case 0x01: /* recommendation E.164 ISDN */ case 0x03: /* recommendation X.121 */ case 0x04: /* telex dial plan */ case 0x08: /* domestic dial plan */ case 0x09: /* private dial plan */ case 0x05: /* reserved */ default: /* reserved */ ast_debug(2, "did info:#2=%X\n", (unsigned)cid->rawdata[x]); break ; } x++; break ; } } return 1; break; default: ast_log(LOG_ERROR, "invalid value in sawflag %d\n", cid->sawflag); } } } if (mylen) { memcpy(cid->oldstuff, buf, mylen * 2); cid->oldlen = mylen * 2; } else cid->oldlen = 0; return 0; }
crc_t flashStateCrc(persistent_config_container_s *state) { return calc_crc((const crc_t*) &state->persistentConfiguration, sizeof(persistent_config_s)); }
short main (short argc, char *argv[]) { unsigned short status=0; /* TX and RX status */ unsigned short tries; /* Attempts to send a file */ unsigned short cmp_size; /* Size after compression */ unsigned short data_written; /* Data written to the file */ unsigned short data_read; /* Data read from the file */ char *file_name; /* filename */ char *function; /* Receive, Transmit */ char *com_port; /* Communications adapter port */ file_name = get_inp (argc, argv); /* Get file name */ if (file_name == NULL ) { disp(); /* Display usage message */ return JM_FNF; } function = get_fun (argc, argv); /* Get function 'R' or 'S' */ if (function == NULL) { disp(); /* Display usage message */ return JM_CMD; } com_port = get_prt (argc, argv); /* Get port '1 to 4 ' */ if (com_port == NULL) { disp(); /* Display usage message */ return JM_CMD; } port = get_port(*com_port); /* Convert port to an offset */ /****************************************************************************/ /* Allocate buffers */ /****************************************************************************/ in_buffer = allocate_memory(DAT_LEN); /* Get some memory for input */ if (in_buffer == NULL) return JM_MEM; /* No memory available */ out_buffer = allocate_memory(DAT_LEN); /* Get some memory for output */ if (out_buffer == NULL) return JM_MEM; /* No memory available */ comp_buffer=allocate_memory(DAT_LEN); /* Get memory for compression */ if (comp_buffer == NULL) return JM_MEM; /* No memory available */ file_buffer=allocate_memory(DAT_LEN); /* Get memory for file buffer */ if (file_buffer == NULL) return JM_MEM; /* No memory available */ int_buffer =allocate_memory(DAT_LEN); /* Memory for interrupt buffer */ if (int_buffer == NULL) return JM_MEM; /* No memory available */ /****************************************************************************/ screen (SCR_SGN,NULL,NULL); /* Write signon screen */ syst.s_len = BLK_SIZ; syst.s_byt = 0; syst.s_blk = 0; syst.s_sta = okay; switch(*function) /* Functions are TX and RX */ { /****************************************************************************/ /* Receive JMODEM file */ /****************************************************************************/ case 'R': { if (!file_io(CREATE, &handle, &file_name, NULL) ) { buff = (JBUF *) in_buffer; /* Assign type JBUF */ open_chan(port); /* Open com channel */ screen (SCR_STA,NULL,NULL); /* Write status block */ status = rx_sync(); /* Synchronize */ if (!status) screen (SCR_SYR,NULL,NULL); data_written = 0xFFFF; tries = 10; /* Attempts to receive */ while ( (data_written) /* Write file okay */ && (!user_abort ) /* No break key */ && (!status ) /* Recev block okay */ && (tries--) ) /* 10 retries */ { time(&start); /* Get starting time */ screen (SCR_SYS,&syst,NULL); /* Show status block */ status = recv_blk ( /* Receive data-block*/ &syst.s_len, /* Block length */ in_buffer); /* Input buffer */ if (status) /* If bad */ break; /* Abort the WHILE */ if( (!(calc_crc(GET_CRC, /* Calculate CRC */ syst.s_len, /* Amount to check */ in_buffer) )) /* Receiver buffer */ && ( buff->blk_num == /* Check block also */ (unsigned char) (syst.s_blk +1))) /* Block number */ { syst.s_sta = okay; /* Text pointer */ tries=10; /* Reset count */ syst.s_len -= OVRHD; /* Subtract overhead */ *out_buffer = ACK; /* Good */ write_chan(1,out_buffer); /* Send the ACK */ /* If data was compressed */ if ( (buff->blk_typ & COMP) == COMP) { syst.s_len = decode ( /* Decode the data */ syst.s_len, /* Data-block length */ &buff->blk_dat, /* Where to start */ file_buffer); /* Where to put data */ } else /* Data was normal (not compressed, just copy ) */ { memcpy (file_buffer,&buff->blk_dat,syst.s_len); } /* Write to the file */ data_written = file_io( WRITE , /* Function */ &handle, /* File handle */ &file_buffer , /* Where data is */ syst.s_len ); /* Amount to write */ syst.s_byt += data_written; /* Total bytes */ syst.s_blk++; /* Block number */ time(&finish); /* Get end time */ if (finish - start) /* Check div/0 */ syst.s_cps = (short) /* Calc Block CPS */ (data_written / (finish - start) ); /* Check for end-of-file */ if ( (buff->blk_typ & EOF_) == EOF_) { /* This was the end of file */ file_io(CLOSE, /* Function */ &handle, /* Open handle */ &file_name, /* Name not used */ NULL); /* Buffer not used */ close_chan(port); /* Close the port */ status = JM_NRM; /* Set status */ goto cleanup; /* exit routine */ } } else { *out_buffer = NAK; /* Bad block */ syst.s_sta = retry; /* Char pointer */ write_chan(1,out_buffer); /* Send the NAK */ } } close_chan(port); /* Aborted */ file_io(DELETE, /* Function */ &handle, /* File handle */ &file_name, /* Name */ NULL); /* Buffer not used */ status = JM_ABT; break; /* Exit if() {} */ } else /* Can't create file */ { status = JM_CRE; break; /* Exit while() {} */ } break; /* Exit case 'R' */ } /****************************************************************************/ /* Send JMODEM file */ /****************************************************************************/ case 'S': /* Send JMODEM file */ { if (!file_io(OPEN_READ, &handle, &file_name, NULL) ) { buff = (JBUF *)out_buffer; /* Assign type JBUF */ syst.s_byt = 0; /* Restore byte count */ open_chan(port); /* Open COM port */ data_read = 0xFFFF; /* Initialize */ screen (SCR_STA,NULL,NULL); /* Write status block */ status = tx_sync(); /* Synchronize */ if (!status) screen (SCR_SYT,NULL,NULL); while ( (!user_abort) /* Ctrl - break */ && (!status) ) /* sent okay */ { time(&start); /* Get starting time */ data_read = file_io( READ , /* Read a record */ &handle , /* File pointer */ &file_buffer , /* Where to put */ syst.s_len ); /* Amount to read */ if (!data_read) /* Past end of file */ break; syst.s_byt += (long) data_read; /* Running count */ screen (SCR_SYS,&syst,NULL); /* Show status block */ buff->blk_num = (unsigned char) ++syst.s_blk; /* Block number */ if (data_read != syst.s_len) /* Byte request */ buff->blk_typ = EOF_; /* Into control-byte */ else buff->blk_typ = NORM; /* Normal block */ cmp_size = encode (data_read, /* Encode size */ file_buffer, /* Source */ comp_buffer); /* Destination */ if ( cmp_size < data_read ) /* If compressed */ { buff->len = (cmp_size+OVRHD); /* Length of block */ buff->blk_typ |= COMP; /* Show compressed */ memcpy (&buff->blk_dat, /* Start of data */ comp_buffer, /* Copy from here */ cmp_size); /* This much */ } else /* Not compressed */ { buff->len = (data_read+OVRHD);/* Length of block */ memcpy (&buff->blk_dat, /* Copy to */ file_buffer, /* Copy from */ data_read); /* This amount */ } calc_crc(SET_CRC, /* Calculate CRC */ buff->len , /* Length of block */ out_buffer); /* Where data is */ status = send_blk( /* Send the block */ buff->len, /* Block length */ &syst, /* Read block ptr. */ out_buffer); /* Buffer pointer */ time(&finish); /* Get end time */ if (finish - start) /* Guard div/zero */ syst.s_cps = (short) /* Calc Block CPS */ (data_read / (finish - start) ); if ( buff->blk_typ == EOF_) /* Last record */ break; } close_chan(port); /* Close the port */ if (status) syst.s_sta = abrt; /* A text pointer */ else syst.s_sta = done; /* A text pointer */ file_io(CLOSE, &handle, &file_name, NULL); /* Close the file */ screen (SCR_SYS,&syst,NULL); /* Show status block */ } else /* File not found */ { status = JM_FNF; } break; /* End of CASE 'S' */ } } cleanup: free (in_buffer); /* Free buffers */ free (out_buffer); free (comp_buffer); free (file_buffer); /* Five-second timer to display error messages */ if (status != JM_NRM) { time(&finish); start = 0; finish += 5; while ( finish > start ) time(&start); } screen (SCR_END,NULL,NULL); /* Clear the screen */ return status; /* Normal exit */ }