static void vivid_vbi_gen_teletext(u8 *packet, unsigned line, unsigned frame) { unsigned offset = 2; unsigned i; packet[0] = hamming[1 + ((line & 1) << 3)]; packet[1] = hamming[line >> 1]; memset(packet + 2, 0x20, 40); if (line == 0) { /* subcode */ packet[2] = hamming[frame % 10]; packet[3] = hamming[frame / 10]; packet[4] = hamming[0]; packet[5] = hamming[0]; packet[6] = hamming[0]; packet[7] = hamming[0]; packet[8] = hamming[0]; packet[9] = hamming[1]; offset = 10; } packet += offset; memcpy(packet, "Page: 100 Row: 10", 17); packet[7] = '0' + frame / 10; packet[8] = '0' + frame % 10; packet[15] = '0' + line / 10; packet[16] = '0' + line % 10; for (i = 0; i < 42 - offset; i++) packet[i] = calc_parity(packet[i]); }
void pce220_serial_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) { if (id == TIMER_SEND && m_enabled) { //send data if (m_bytes_count <= length()) { switch(m_state) { case SIO_WAIT: m_ack = 1; //data send request //waits until is ready to receive if(!m_busy) return; break; case SIO_SEND_START: //send start bit m_xin = 1; break; case SIO_SEND_BIT0: case SIO_SEND_BIT1: case SIO_SEND_BIT2: case SIO_SEND_BIT3: case SIO_SEND_BIT4: case SIO_SEND_BIT5: case SIO_SEND_BIT6: case SIO_SEND_BIT7: m_xin = BIT(~m_current_byte, m_state - SIO_SEND_BIT0); break; case SIO_SEND_PARITY: m_xin = calc_parity(m_current_byte); break; case SIO_SEND_STOP1: case SIO_SEND_STOP2: //stop bit m_xin = m_ack = 0; break; } if (m_state == (SIO_SEND_PARITY + SIO_STOP_BIT)) { //next byte m_bytes_count++; popmessage("Send %d/%d bytes\n", m_bytes_count , (UINT32)length()); m_state = SIO_WAIT; if (m_bytes_count < length()) fread(&m_current_byte, 1); else m_current_byte = SIO_EOT_BYTE; } else { m_state = get_next_state(); } } else { m_din = m_xin = m_ack = 0; } } else if (id == TIMER_RECEIVE && m_enabled) { //receive data switch(m_state) { case SIO_WAIT: //wait send request if(m_busy) return; m_ack = 1; //acknowledge break; case SIO_SEND_START: //wait for the start bit if (!m_xout) return; break; case SIO_SEND_BIT0: case SIO_SEND_BIT1: case SIO_SEND_BIT2: case SIO_SEND_BIT3: case SIO_SEND_BIT4: case SIO_SEND_BIT5: case SIO_SEND_BIT6: case SIO_SEND_BIT7: m_current_byte |= ((~m_xout)&1) << (m_state - SIO_SEND_BIT0); break; case SIO_SEND_PARITY: if (m_xout != calc_parity(m_current_byte)) logerror("SIO %s: byte %d has wrong parity!\n", tag(), m_bytes_count); break; case SIO_SEND_STOP1: case SIO_SEND_STOP2: //receive stop bit m_ack = 0; break; } if (m_state == (SIO_SEND_PARITY + SIO_STOP_BIT)) { //next byte m_bytes_count++; popmessage("Received %d bytes\n", m_bytes_count); m_state = SIO_WAIT; if (m_current_byte != SIO_EOT_BYTE) fwrite(&m_current_byte, 1); m_current_byte = 0; } else { m_state = get_next_state(); } } else { m_din = m_xin = m_ack = 0; } }
int main(int argc,char** argv){ msg r, t; //len, payload frame f; //seq_nr, *payload, checksum init(HOST,PORT); //--------------------------fisier de log--------------------------------------- int log; char message_r[40] = {"[RECEIVER] Am primit urmatorul pachet:\n"}; char message_ack[40] = {"[RECEIVER] Trimit ACK: "}; char message_nack[] = {"[RECEIVER]Am calculat checksum, nu corespunde. \ Deci trimit ACK pentru Seq_Nr: "}; char data[12], timp[10], buffer[300]; char new_line[60] = {"-------------------------------------------------\n"}; //---------------------creez fisier de out-------------------------------------- char filename[50]; /* primesc ca argument numele fisierului de out */ strcpy(filename, argv[1]); /* sau scriu aici numele fisierului de out */ //strcpy(filename, "fisier1.out"); int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644); lseek(fd, 0, SEEK_SET); //----------------------structura pentru data si timp--------------------------- time_t now; struct tm *timeinfo; //-------------------incep sa primesc mesaje------------------------------------ char seq_nr = 0; char parity; char cs_buffer[9]; srand(time(NULL)); int deelay; while(1) { /* intarziere pachete la un numar random generat doar daca este 5 */ deelay = rand() % 10 + 1; if(deelay == 5){ usleep(100000); } if (recv_message(&r) < 0){ perror("Receive message"); return -1; } f.seq_nr = r.payload[0]; f.checksum = r.payload[1]; get_binary_checksum(f.checksum, cs_buffer); f.payload = (char*)malloc((r.len + 1) * sizeof(char)); memset(f.payload, 0, (r.len + 1) * sizeof(char)); memcpy(f.payload, r.payload + 2, r.len * sizeof(char)); /*------------------obtin ora curenta-----------------*/ time(&now); timeinfo = localtime(&now); sprintf(data, "%02d-%02d-%02d", timeinfo->tm_mday, 1 + timeinfo->tm_mon, 1900 + timeinfo->tm_year); sprintf(timp, "%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); /*----creez mesaj pentru fisier de log daca se primeste corect----*/ sprintf(buffer, "[%s %s] %sSeq Nr: %d\nPayload: %s\nChecksum: %s\n%s", data, timp, message_r, f.seq_nr, f.payload, cs_buffer, new_line); log = open("log.txt", O_WRONLY | O_APPEND); write(log, buffer, strlen(buffer)); close(log); /*----------------------------------------------------*/ if(r.len == 100 && f.checksum == -1) { f.seq_nr = seq_nr++; f.checksum = 255; get_binary_checksum(f.checksum, cs_buffer); f.payload = (char*)malloc(sizeof(char)); f.payload = ""; memcpy(t.payload, &f.seq_nr, sizeof(char)); memcpy(t.payload + 1, &f.checksum, sizeof(char)); memcpy(t.payload + 2, f.payload, sizeof(char)); t.len = 100; send_message(&t); break; } parity = calc_parity(f.payload, r.len); if(f.seq_nr == seq_nr && parity == f.checksum){ /* scriu in fisierul de out ce am primit */ write(fd, f.payload, r.len); f.seq_nr = seq_nr++; f.checksum = parity; get_binary_checksum(f.checksum, cs_buffer); free(f.payload); f.payload = (char*)malloc(sizeof(char)); f.payload = ""; memcpy(t.payload, &f.seq_nr, sizeof(char)); //pun frameul in t memcpy(t.payload + 1, &f.checksum, sizeof(char)); memcpy(t.payload + 2, f.payload, sizeof(char)); t.len = strlen(t.payload + 1); send_message(&t); /*------------------obtin ora curenta-----------------*/ time(&now); timeinfo = localtime(&now); sprintf(data, "%02d-%02d-%02d", timeinfo->tm_mday, 1 + timeinfo->tm_mon, 1900 + timeinfo->tm_year); sprintf(timp, "%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); /*----creez mesaj pentru fisier de log daca se primeste corect----*/ sprintf(buffer, "[%s %s] %s%d\n%s", data, timp, message_ack, f.seq_nr, new_line); log = open("log.txt", O_WRONLY | O_APPEND); write(log, buffer, strlen(buffer)); close(log); } else{ //trimit ACK pe secventa anterioara sprintf(buffer, "[%s %s] %s %d\n%s",data, timp, message_nack, seq_nr, new_line); log = open("log.txt", O_WRONLY | O_APPEND); write(log, buffer, strlen(buffer)); close(log); /*--------------creez mesaj pentru confirmare----------*/ f.seq_nr = seq_nr - 1; f.checksum = parity; get_binary_checksum(f.checksum, cs_buffer); f.payload = (char*)malloc(sizeof(char)); f.payload = ""; memcpy(t.payload, &f.seq_nr, sizeof(char)); memcpy(t.payload + 1, &f.checksum, sizeof(char)); memcpy(t.payload + 2, f.payload, sizeof(char)); t.len = strlen(t.payload + 1); send_message(&t); } } //----------------------------------------------------------------------------- close(fd); return 0; }
void vivid_vbi_gen_sliced(struct vivid_vbi_gen_data *vbi, bool is_60hz, unsigned seqnr) { struct v4l2_sliced_vbi_data *data0 = vbi->data; struct v4l2_sliced_vbi_data *data1 = vbi->data + 1; unsigned frame = seqnr % 60; memset(vbi->data, 0, sizeof(vbi->data)); if (!is_60hz) { unsigned i; for (i = 0; i <= 11; i++) { data0->id = V4L2_SLICED_TELETEXT_B; data0->line = 7 + i; vivid_vbi_gen_teletext(data0->data, i, frame); data0++; } data0->id = V4L2_SLICED_WSS_625; data0->line = 23; /* 4x3 video aspect ratio */ data0->data[0] = 0x08; data0++; for (i = 0; i <= 11; i++) { data0->id = V4L2_SLICED_TELETEXT_B; data0->field = 1; data0->line = 7 + i; vivid_vbi_gen_teletext(data0->data, 12 + i, frame); data0++; } return; } data0->id = V4L2_SLICED_CAPTION_525; data0->line = 21; data1->id = V4L2_SLICED_CAPTION_525; data1->field = 1; data1->line = 21; if (frame < 15) { data0->data[0] = calc_parity(vivid_cc_sequence1[2 * frame]); data0->data[1] = calc_parity(vivid_cc_sequence1[2 * frame + 1]); } else if (frame >= 30 && frame < 45) { frame -= 30; data0->data[0] = calc_parity(vivid_cc_sequence2[2 * frame]); data0->data[1] = calc_parity(vivid_cc_sequence2[2 * frame + 1]); } else { data0->data[0] = calc_parity(0); data0->data[1] = calc_parity(0); } frame = seqnr % (30 * 60); switch (frame) { case 0: vivid_vbi_gen_set_time_of_day(vbi->time_of_day_packet); /* fall through */ case 1 ... 7: data1->data[0] = vbi->time_of_day_packet[frame * 2]; data1->data[1] = vbi->time_of_day_packet[frame * 2 + 1]; break; default: data1->data[0] = calc_parity(0); data1->data[1] = calc_parity(0); break; } }
static void vivid_vbi_gen_set_time_of_day(u8 *packet) { struct tm tm; u8 checksum, i; time_to_tm(get_seconds(), 0, &tm); packet[0] = calc_parity(0x07); packet[1] = calc_parity(0x01); packet[2] = calc_parity(0x40 | tm.tm_min); packet[3] = calc_parity(0x40 | tm.tm_hour); packet[4] = calc_parity(0x40 | tm.tm_mday); if (tm.tm_mday == 1 && tm.tm_mon == 2 && sys_tz.tz_minuteswest > tm.tm_min + tm.tm_hour * 60) packet[4] = calc_parity(0x60 | tm.tm_mday); packet[5] = calc_parity(0x40 | (1 + tm.tm_mon)); packet[6] = calc_parity(0x40 | (1 + tm.tm_wday)); packet[7] = calc_parity(0x40 | ((tm.tm_year - 90) & 0x3f)); packet[8] = calc_parity(0x0f); for (checksum = i = 0; i <= 8; i++) checksum += packet[i] & 0x7f; packet[9] = calc_parity(0x100 - checksum); checksum = 0; packet[10] = calc_parity(0x07); packet[11] = calc_parity(0x04); if (sys_tz.tz_minuteswest >= 0) packet[12] = calc_parity(0x40 | ((sys_tz.tz_minuteswest / 60) & 0x1f)); else packet[12] = calc_parity(0x40 | ((24 + sys_tz.tz_minuteswest / 60) & 0x1f)); packet[13] = calc_parity(0); packet[14] = calc_parity(0x0f); for (checksum = 0, i = 10; i <= 14; i++) checksum += packet[i] & 0x7f; packet[15] = calc_parity(0x100 - checksum); }