void Tag_Actions::select_mifare_card() { packet_reset(); packet[2]=0x02; packet[3]=0x10; checksum(); cout<<"Sending the packet: \n"; for(int i=0;i<packet[2]+3;i++) { printf("%x\n",packet[i]); serial.WriteByte((char)packet[i]); } cout<<"\nReading...\n"; Read_rfid(); cout<<"Serial Number: "; for(int i=5;i<9;i++) printf("%x\t",packet_received[i]); cout<<endl; cout<<"Type: "; if(packet_received[9]==0x00) cout<<"Mifare Standard 1K(S50) card\n"; else if(packet_received[9]==0x01) cout<<"Mifare Standard 4K(S70) card\n"; else if(packet_received[9]==0x02) cout<<"Mifare ProX card\n"; }
void Tag_Actions::decrement_value(char key_type, byte block, byte *key, byte *value) { packet_reset(); packet[2]=0x0E; packet[3]=0x16; if(key_type=='a' || 'A') packet[4]=0x00; else if(key_type=='b' || 'B') packet[4]=0x01; packet[5]=block; for(int i=6;i<12;i++) packet[i]=key[i-6]; for(int i=12;i<16;i++) packet[i]=value[i-12]; checksum(); cout<<"Sending the packet: \n"; for(int i=0;i<packet[2]+3;i++) { printf("%x\n",packet[i]); serial.WriteByte((char)packet[i]); } cout<<"\nReading...\n"; Read_rfid(); if(packet_received[4]==0x00) cout<<"Decrement value block success!!"; else cout<<"Oops!! Error in writing."; }
void Tag_Actions::read_value_block(char key_type, byte block, byte *key) { packet_reset(); packet[2]=0x0A; packet[3]=0x14; if(key_type=='a' || 'A') packet[4]=0x00; else if(key_type=='b' || 'B') packet[4]=0x01; packet[5]=block; for(int i=6;i<12;i++) packet[i]=key[i-6]; checksum(); cout<<"Sending the packet: \n"; for(int i=0;i<packet[2]+3;i++) { printf("%x\n",packet[i]); serial.WriteByte((char)packet[i]); } cout<<"\nReading...\n"; Read_rfid(); cout<<"Thus the data in the block is: \n"; for(int i=5;i<9;i++) printf("%x\n",packet_received[i]); }
static PyObject * Lexer_reset(LexerObject *self) { packet_reset(&self->lexer); if (PyErr_Occurred()) return NULL; return 0; }
static void handle_invalid_packet(struct client_context *client) { int port = 0; char remote[INET_ADDRSTRLEN + 1]; port = ip_name(&client->addr, remote, sizeof(remote)); logger_log(LOG_ERR, "Invalid tcp packet from %s:%d", remote, port); packet_reset(&client->packet); close_client(client); }
static struct client_context * new_client(int mtu) { struct client_context *client = malloc(sizeof(*client)); memset(client, 0, sizeof(*client)); client->packet.buf = malloc(PRIMITIVE_BYTES + mtu); client->packet.max = PRIMITIVE_BYTES + mtu; packet_reset(&client->packet); return client; }
struct remote_context * new_remote(uint16_t timeout) { struct remote_context *remote = malloc(sizeof(*remote)); memset(remote, 0, sizeof(*remote)); remote->timer = malloc(sizeof(uv_timer_t)); remote->idle_timeout = timeout; remote->packet.max = MAX_PACKET_SIZE - HEADER_BYTES; packet_reset(&remote->packet); return remote; }
static LexerObject * newLexerObject(PyObject *arg) { LexerObject *self; self = PyObject_New(LexerObject, &Lexer_Type); if (self == NULL) return NULL; memset(&self->lexer, 0, sizeof(struct gps_packet_t)); packet_reset(&self->lexer); return self; }
// TODO: Remove all this and replace with a custom format that is sent to vu1 for // transformation, lighting and being able to deal with other prim types! MF_API void MFBegin(uint32 vertexCount) { MFCALLSTACK; packet_reset(&packet); beginCount = vertexCount; currentVert = 0; MFMat_Standard_Data *pData = (MFMat_Standard_Data*)pSetMaterial->pInstanceData; MFTexture *pTexture = pData->pTextures[pData->diffuseMapIndex]; width = pTexture->pTemplateData->pSurfaces[0].width; height = pTexture->pTemplateData->pSurfaces[0].height; int tw = mylog2(width); int th = mylog2(height); int num = vertexCount*2 + 9; packet_append_64(&packet, DMA_SET_TAG(num, 0, DMA_TAG_END, 0, 0, 0)); packet_append_64(&packet, 0); packet_append_64(&packet, GIF_SET_TAG(num-1, 1, 0, 0, 0, 1 )); packet_append_64(&packet, GIF_AD); /* GIF_SET_TEX0(tex base, tex width, tex psm, width, height, texcure alpha component, texture function, CLUT stuff */ packet_append_64(&packet, GIF_SET_TEX0( tex_addr>>8, width >> 6, 0,tw,th,1,1,0,0,0,0,0)); packet_append_64(&packet, GIF_REG_TEX0_1); packet_append_64(&packet, GIF_SET_TEX1(1, 0, 1, 1, 0, 0, 0)); packet_append_64(&packet, GIF_REG_TEX1_1); packet_append_64(&packet, GIF_SET_TEXA(0, 1, 255)); packet_append_64(&packet, GIF_REG_TEXA); packet_append_64(&packet, GIF_SET_CLAMP(0, 0, 0, 0, 0, 0)); packet_append_64(&packet, GIF_REG_CLAMP_1); float one = 1.0f; /* cludgy way to obtain 1.0f as a u32 */ packet_append_64(&packet, GIF_SET_RGBAQ( r, g, b, a, *((int*)(&one)) ) ); packet_append_64(&packet, GIF_REG_RGBAQ); packet_append_64(&packet, GIF_SET_ALPHA(0, 1,0,1,0)); packet_append_64(&packet, GIF_REG_ALPHA_1); /* GIF_SET_PRIM(prim type, Shading, Texture, Fog, Alpha, AA, STQ/UV, context, fragmtnt*/ packet_append_64(&packet, GIF_SET_PRIM (6, 0, 1, 0, 1, 0, 1, 0, 0)); packet_append_64(&packet, GIF_REG_PRIM); }
void Tag_Actions::control_rf_transmit(bool state_switch) { packet_reset(); packet[2]=0x03; //length packet[3]=0x01; //command for RF transmit if(state_switch==true) packet[4]=0x01; else packet[4]=0x00; checksum(); cout<<"Sending the packet: \n"; for(int i=0;i<packet[2]+3;i++) { printf("%x\n",packet[i]); serial.WriteByte((char)packet[i]); } cout<<"\nReading...\n"; Read_rfid(); }
static int Lexer_init(LexerObject *self) { packet_reset(&self->lexer); return 0; }
void gpsd_set_speed(struct gps_device_t *session, speed_t speed, char parity, unsigned int stopbits) { speed_t rate; /* * Yes, you can set speeds that aren't in the hunt loop. If you * do this, and you aren't on Linux where baud rate is preserved * across port closings, you've screwed yourself. Don't do that! */ if (speed < 300) rate = B0; else if (speed < 1200) rate = B300; else if (speed < 2400) rate = B1200; else if (speed < 4800) rate = B2400; else if (speed < 9600) rate = B4800; else if (speed < 19200) rate = B9600; else if (speed < 38400) rate = B19200; else if (speed < 57600) rate = B38400; else if (speed < 115200) rate = B57600; else rate = B115200; if (rate != cfgetispeed(&session->ttyset) || parity != session->gpsdata.dev.parity || stopbits != session->gpsdata.dev.stopbits) { /* * Don't mess with this conditional! Speed zero is supposed to mean * to leave the port speed at whatever it currently is. This leads * to excellent behavior on Linux, which preserves baudrate across * serial device closes - it means that if you've opended this * device before you typically don't have to hunt at all because * it's still at the same speed you left it - you'll typically * get packet lock within 1.5 seconds. Alas, the BSDs and OS X * aren't so nice. */ /*@ignore@*/ if (rate != B0) { (void)cfsetispeed(&session->ttyset, rate); (void)cfsetospeed(&session->ttyset, rate); } /*@end@*/ session->ttyset.c_iflag &= ~(PARMRK | INPCK); session->ttyset.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD); session->ttyset.c_cflag |= (stopbits == 2 ? CS7 | CSTOPB : CS8); switch (parity) { case 'E': case (char)2: session->ttyset.c_iflag |= INPCK; session->ttyset.c_cflag |= PARENB; break; case 'O': case (char)1: session->ttyset.c_iflag |= INPCK; session->ttyset.c_cflag |= PARENB | PARODD; break; } if (tcsetattr(session->gpsdata.gps_fd, TCSANOW, &session->ttyset) != 0) return; /* * Serious black magic begins here. Getting this code wrong can cause * failures to lock to a correct speed, and not clean reproducible * failures but flukey hardware- and timing-dependent ones. So * be very sure you know what you're doing before hacking it, and * test thoroughly. * * The fundamental problem here is that serial devices take time * to settle into a new baud rate after tcsetattr() is issued. Until * they do so, input will be arbitarily garbled. Normally this * is not a big problem, but in our hunt loop the garbling can trash * a long enough prefix of each sample to prevent detection of a * packet header. We could address the symptom by making the sample * size enough larger that subtracting the maximum length of garble * would still leave a sample longer than the maximum packet size. * But it's better (and more efficient) to address the disease. * * In theory, one might think that not even a tcflush() call would * be needed, with tcsetattr() delaying its return until the device * is in a good state. For simple devices like a 14550 UART that * have fixed response timings this may even work, if the driver * writer was smart enough to delay the return by the right number * of milliseconds after poking the device port(s). * * Problems may arise if the driver's timings are off. Or we may * be talking to a USB device like the pl2303 commonly used in GPS * mice; on these, the change will not happen immediately because * it has to be sent as a message to the external processor that * has to act upon it, and that processor may still have buffered * data in its own FIFO. In this case the expected delay may be * too large and too variable (depending on the details of how the * USB device is integrated with its symbiont hardware) to be put * in the driver. * * So, somehow, we have to introduce a delay after tcsatattr() * returns sufficient to allow *any* device to settle. On the other * hand, a really long delay will make gpsd device registration * unpleasantly laggy. * * The classic way to address this is with a tcflush(), counting * on it to clear the device FIFO. But that call may clear only the * kernel buffers, not the device's hardware FIFO, so it may not * be sufficient by itself. * * flush followed by a 200-millisecond delay followed by flush has * been found to work reliably on the pl2303. It is also known * from testing that a 100-millisec delay is too short, allowing * occasional failure to lock. */ (void)tcflush(session->gpsdata.gps_fd, TCIOFLUSH); (void)usleep(200000); (void)tcflush(session->gpsdata.gps_fd, TCIOFLUSH); } gpsd_report(LOG_INF, "speed %u, %d%c%d\n", gpsd_get_speed(&session->ttyset), 9 - stopbits, parity, stopbits); session->gpsdata.dev.baudrate = (unsigned int)speed; session->gpsdata.dev.parity = parity; session->gpsdata.dev.stopbits = stopbits; /* * The device might need a wakeup string before it will send data. * If we don't know the device type, ship it every driver's wakeup * in hopes it will respond. But not to USB or Bluetooth, because * shipping probe strings to unknown USB serial adaptors or * Bluetooth devices may spam devices that aren't GPSes at all and * could become confused. */ if (!session->context->readonly && session->sourcetype != source_usb && session->sourcetype != source_bluetooth) { if (isatty(session->gpsdata.gps_fd) != 0 && !session->context->readonly) { const struct gps_type_t **dp; if (session->device_type == NULL) { for (dp = gpsd_drivers; *dp; dp++) if ((*dp)->event_hook != NULL) (*dp)->event_hook(session, event_wakeup); } else if (session->device_type->event_hook != NULL) session->device_type->event_hook(session, event_wakeup); } } packet_reset(&session->packet); }
void receive_from_source(struct source_context *source) { packet_reset(&source->packet); source->handle.stream.data = source; uv_read_start(&source->handle.stream, source_alloc_cb, source_recv_cb); }
void receive_from_remote(struct remote_context *remote) { packet_reset(&remote->packet); remote->handle.stream.data = remote; uv_read_start(&remote->handle.stream, remote_alloc_cb, remote_recv_cb); }
static void recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { struct tundev_context *ctx; struct client_context *client; ctx = stream->data; client = container_of(stream, struct client_context, handle.stream); struct packet *packet = &client->packet; if (nread > 0) { if ((ctx->connect == AUTHING) && ( (0 == memcmp(packet->buf, "GET ", 4)) || (0 == memcmp(packet->buf, "POST", 4)) ) ) { http_auth(stream, packet->buf); packet_reset(packet); ctx->connect = CONNECTED; return; } int rc = packet_filter(packet, buf->base, nread); if (rc == PACKET_UNCOMPLETE) { return; } else if (rc == PACKET_INVALID) { logger_log(LOG_ERR, "Filter Invalid: %d", nread); goto error; } int clen = packet->size; int mlen = packet->size - PRIMITIVE_BYTES; uint8_t *c = packet->buf, *m = packet->buf; assert(mlen > 0 && mlen <= ctx->tun->mtu); int err = crypto_decrypt(m, c, clen); if (err) { logger_log(LOG_ERR, "Fail Decrypt: %d", clen); goto error; } struct iphdr *iphdr = (struct iphdr *) m; in_addr_t client_network = iphdr->saddr & htonl(ctx->tun->netmask); if (client_network != ctx->tun->network) { char *a = inet_ntoa(*(struct in_addr *) &iphdr->saddr); logger_log(LOG_ERR, "Invalid client: %s", a); close_client(client); return; } if (client->peer == NULL) { uv_rwlock_rdlock(&rwlock); struct peer *peer = lookup_peer(iphdr->saddr, peers); uv_rwlock_rdunlock(&rwlock); if (peer == NULL) { char saddr[24] = {0}, daddr[24] = {0}; parse_addr(iphdr, saddr, daddr); logger_log(LOG_WARNING, "[TCP] Cache miss: %s -> %s", saddr, daddr); uv_rwlock_wrlock(&rwlock); peer = save_peer(iphdr->saddr, &client->addr, peers); uv_rwlock_wrunlock(&rwlock); } else { if (peer->data) { struct client_context *old = peer->data; close_client(old); } } peer->protocol= xTUN_TCP; peer->data = client; client->peer = peer; } network_to_tun(ctx->tunfd, m, mlen); packet_reset(packet); } else if (nread < 0) { if (nread != UV_EOF) { logger_log(LOG_ERR, "Receive from client failed: %s", uv_strerror(nread)); } close_client(client); } return; error: if (verbose) { dump_hex(buf->base, nread, "Invalid tcp Packet"); } handle_invalid_packet(client); }