int Layer7_Connection::X_Memory_Write_Block (memaddr_t addr, const CArray & data) { CArray prev; unsigned int i, j; int k, res = 0; const unsigned blocksize = 12; if (X_Memory_Read_Block (addr, data (), prev) == -1) return -1; for (i = 0; i < data (); i++) { if (data[i] == prev[i]) continue; j = 0; while (data[i + j] != prev[i + j] && j < blocksize && i + j < data ()) j++; k = X_Memory_Write (addr + i, CArray (data.array () + i, j)); if (k == -1) return -1; if (k == -2) res = -2; i += j - 1; } return res; }
template <class type> void Vector<type>::Clear(const type &T) { UINT Length = Data.size(); type *CPtr = CArray(); for(UINT i = 0; i < Length; i++) { CPtr[i] = T; } }
bool EIBNetIPTunnel::leaveBusmonitor () { mode = 0; connect_busmonitor = 0; inqueue.put (CArray ()); pth_sem_inc (&insignal, 1); return 1; }
template <class type> void Vector<type>::Scale(const type &t) { const UINT Length = Data.size(); type *CPtr = CArray(); for(UINT Index = 0; Index < Length; Index++) { CPtr[Index] *= t; } }
bool EIBNetIPTunnel::enterBusmonitor () { mode = 1; if (support_busmonitor) connect_busmonitor = 1; inqueue.put (CArray ()); pth_sem_inc (&insignal, 1); return 1; }
template <class type> type Vector<type>::Sum() const { const UINT Length = Data.size(); const type *CPtr = CArray(); type Result = CPtr[0]; for(UINT Index = 1; Index < Length; Index++) { Result += CPtr[Index]; } return Result; }
bool EMI2Layer2::enterBusmonitor () { if (!Layer2::enterBusmonitor ()) return false; const uchar t1[] = { 0xa9, 0x1E, 0x12, 0x34, 0x56, 0x78, 0x9a }; const uchar t2[] = { 0xa9, 0x90, 0x18, 0x34, 0x45, 0x67, 0x8a }; TRACEPRINTF (t, 2, this, "OpenBusmon"); iface->SendReset (); iface->Send_Packet (CArray (t1, sizeof (t1))); iface->Send_Packet (CArray (t2, sizeof (t2))); if (!iface->Send_Queue_Empty ()) { pth_event_t e = pth_event (PTH_EVENT_SEM, iface->Send_Queue_Empty_Cond ()); pth_wait (e); pth_event_free (e, PTH_FREE_THIS); } return true; }
template <class type> UINT Vector<type>::MinIndex() const { const UINT Length = Data.size(); const type *CPtr = CArray(); UINT SmallestIndexSeen = 0; for(UINT Index = 1; Index < Length; Index++) { if(CPtr[Index] < CPtr[SmallestIndexSeen]) { SmallestIndexSeen = Index; } } return SmallestIndexSeen; }
template <class type> UINT Vector<type>::MaxIndex() const { const UINT Length = Data.size(); const type *CPtr = CArray(); UINT LargestIndexSeen = 0; for(UINT Index = 1; Index < Length; Index++) { if(CPtr[Index] > CPtr[LargestIndexSeen]) { LargestIndexSeen = Index; } } return LargestIndexSeen; }
bool EMI1Layer2::Close () { if (!Layer2::Close ()) return false; TRACEPRINTF (t, 2, this, "CloseL2"); uchar t[] = { 0x46, 0x01, 0x00, 0x60, 0xc0 }; iface->Send_Packet (CArray (t, sizeof (t))); if (!iface->Send_Queue_Empty ()) { pth_event_t e = pth_event (PTH_EVENT_SEM, iface->Send_Queue_Empty_Cond ()); pth_wait (e); pth_event_free (e, PTH_FREE_THIS); } return true; }
bool EMI2Layer2::leaveBusmonitor () { if (!Layer2::leaveBusmonitor ()) return false; TRACEPRINTF (t, 2, this, "CloseBusmon"); uchar t[] = { 0xa9, 0x1E, 0x12, 0x34, 0x56, 0x78, 0x9a }; iface->Send_Packet (CArray (t, sizeof (t))); while (!iface->Send_Queue_Empty ()) { pth_event_t e = pth_event (PTH_EVENT_SEM, iface->Send_Queue_Empty_Cond ()); pth_wait (e); pth_event_free (e, PTH_FREE_THIS); } return true; }
int Layer7_Connection::A_Memory_Write_Block (memaddr_t addr, const CArray & data) { CArray prev; int i, j, k, res = 0; const unsigned blocksize = 12; for (i = 0; i < data (); i += blocksize) { j = blocksize; if (i + j > data ()) j = data () - i; k = A_Memory_Write (addr + i, CArray (data.array () + i, j)); if (k == -1) return -1; } return res; }
bool EMI1Layer2::Open () { if (!Layer2::Open ()) return false; const uchar t1[] = { 0x46, 0x01, 0x00, 0x60, 0x12 }; TRACEPRINTF (t, 2, this, "OpenL2"); iface->SendReset (); iface->Send_Packet (CArray (t1, sizeof (t1))); while (!iface->Send_Queue_Empty ()) { pth_event_t e = pth_event (PTH_EVENT_SEM, iface->Send_Queue_Empty_Cond ()); pth_wait (e); pth_event_free (e, PTH_FREE_THIS); } return true; }
void TPUARTwrap::RecvLPDU (const uchar * data, int len) { t->TracePacket (1, "RecvLP", len, data); if (state == T_busmonitor) { LBusmonPtr l = LBusmonPtr(new L_Busmonitor_PDU ()); l->pdu.set (data, len); recv_L_Busmonitor (std::move(l)); } else if (state > T_start) { LPDUPtr l = LPDU::fromPacket (CArray (data, len), t); if (l->getType () != L_Data) TRACEPRINTF (t, 1, "dropping packet: type %d", l->getType ()); else { if (((L_Data_PDU *)(&*l))->valid_checksum) recv_L_Data (dynamic_unique_cast<L_Data_PDU>(std::move(l))); else TRACEPRINTF (t, 1, "dropping packet: invalid"); } } }
void TPUARTLayer2Driver::Run (pth_sem_t * stop1) { struct message m; int l; pth_event_t stop = pth_event (PTH_EVENT_SEM, stop1); pth_event_t input = pth_event (PTH_EVENT_SEM, &in_signal); while (pth_event_status (stop) != PTH_STATUS_OCCURRED) { pth_event_concat (stop, input, NULL); l = pth_read_ev (fd, &m, sizeof (m), stop); if (l >= 0) { LPDU *l1; if (m.length > sizeof (m.data)) m.length = sizeof (m.data); t->TracePacket (0, this, "Recv", m.length, m.data); if (vmode && mode == 0) { L_Busmonitor_PDU *l2 = new L_Busmonitor_PDU; l2->pdu.set (m.data, m.length); outqueue.put (l2); pth_sem_inc (&out_signal, 1); } if (mode == 0) l1 = LPDU::fromPacket (CArray (m.data, m.length)); else { l1 = new L_Busmonitor_PDU; ((L_Busmonitor_PDU *) l1)->pdu.set (m.data, m.length); } outqueue.put (l1); pth_sem_inc (&out_signal, 1); } pth_event_isolate (stop); if (!inqueue.isempty ()) { LPDU *l1 = inqueue.top (); CArray c = l1->ToPacket (); unsigned len = c (); if (len > sizeof (m.data)) len = sizeof (m.data); memcpy (m.data, c.array (), len); m.length = len; if (ver) m.length--; t->TracePacket (0, this, "Send", m.length, m.data); l = pth_write_ev (fd, &m, sizeof (m), stop); if (l >= 0) { if (vmode) { L_Busmonitor_PDU *l2 = new L_Busmonitor_PDU; l2->pdu.set (c); outqueue.put (l2); pth_sem_inc (&out_signal, 1); } pth_sem_dec (&in_signal); delete inqueue.get (); } } } pth_event_free (stop, PTH_FREE_THIS); pth_event_free (input, PTH_FREE_THIS); }
void FT12LowLevelDriver::Run (pth_sem_t * stop1) { CArray last; int i; uchar buf[255]; pth_event_t stop = pth_event (PTH_EVENT_SEM, stop1); pth_event_t input = pth_event (PTH_EVENT_SEM, &in_signal); pth_event_t timeout = pth_event (PTH_EVENT_RTIME, pth_time (0, 100000)); while (pth_event_status (stop) != PTH_STATUS_OCCURRED) { pth_event_isolate (input); pth_event_isolate (timeout); if (mode == 0) pth_event_concat (stop, input, NULL); if (mode == 1) pth_event_concat (stop, timeout, NULL); i = pth_read_ev (fd, buf, sizeof (buf), stop); if (i > 0) { t->TracePacket (0, this, "Recv", i, buf); akt.setpart (buf, akt (), i); } while (akt.len () > 0) { if (akt[0] == 0xE5 && mode == 1) { pth_sem_dec (&in_signal); inqueue.get (); if (inqueue.isempty ()) pth_sem_set_value (&send_empty, 1); akt.deletepart (0, 1); mode = 0; repeatcount = 0; } else if (akt[0] == 0x10) { if (akt () < 4) break; if (akt[1] == akt[2] && akt[3] == 0x16) { uchar c1 = 0xE5; t->TracePacket (0, this, "Send Ack", 1, &c1); write (fd, &c1, 1); if ((akt[1] == 0xF3 && !recvflag) || (akt[1] == 0xD3 && recvflag)) { //right sequence number recvflag = !recvflag; } if ((akt[1] & 0x0f) == 0) { const uchar reset[1] = { 0xA0 }; CArray *c = new CArray (reset, sizeof (reset)); t->TracePacket (0, this, "RecvReset", *c); outqueue.put (c); pth_sem_inc (&out_signal, TRUE); } } akt.deletepart (0, 4); } else if (akt[0] == 0x68) { int len; uchar c1; if (akt () < 7) break; if (akt[1] != akt[2] || akt[3] != 0x68) { //receive error, try to resume akt.deletepart (0, 1); continue; } if (akt () < akt[1] + 6) break; c1 = 0; for (i = 4; i < akt[1] + 4; i++) c1 += akt[i]; if (akt[akt[1] + 4] != c1 || akt[akt[1] + 5] != 0x16) { len = akt[1] + 6; //Forget wrong short frame akt.deletepart (0, len); continue; } c1 = 0xE5; t->TracePacket (0, this, "Send Ack", 1, &c1); i = write (fd, &c1, 1); if ((akt[4] == 0xF3 && recvflag) || (akt[4] == 0xD3 && !recvflag)) { if (CArray (akt.array () + 5, akt[1] - 1) != last) { TRACEPRINTF (t, 0, this, "Sequence jump"); recvflag = !recvflag; } else TRACEPRINTF (t, 0, this, "Wrong Sequence"); } if ((akt[4] == 0xF3 && !recvflag) || (akt[4] == 0xD3 && recvflag)) { recvflag = !recvflag; CArray *c = new CArray; len = akt[1] + 6; c->setpart (akt.array () + 5, 0, len - 7); last = *c; outqueue.put (c); pth_sem_inc (&out_signal, TRUE); } akt.deletepart (0, len); } else //Forget unknown byte akt.deletepart (0, 1); } if (mode == 1 && pth_event_status (timeout) == PTH_STATUS_OCCURRED) mode = 0; if (mode == 0 && !inqueue.isempty ()) { const CArray & c = inqueue.top (); t->TracePacket (0, this, "Send", c); repeatcount++; i = pth_write_ev (fd, c.array (), c (), stop); if (i == c ()) { mode = 1; timeout = pth_event (PTH_EVENT_RTIME | PTH_MODE_REUSE, timeout, pth_time (0, 100000)); } } } pth_event_free (stop, PTH_FREE_THIS); pth_event_free (timeout, PTH_FREE_THIS); pth_event_free (input, PTH_FREE_THIS); }
LowLevelDriver * initUSBDriver (LowLevelDriver * i, Trace * tr) { CArray r1, *r = 0; LowLevelDriver *iface; uchar emiver; int cnt = 0; const uchar ask[64] = { 0x01, 0x13, 0x09, 0x00, 0x08, 0x00, 0x01, 0x0f, 0x01, 0x00, 0x00, 0x01 }; uchar init[64] = { 0x01, 0x13, 0x0a, 0x00, 0x08, 0x00, 0x02, 0x0f, 0x03, 0x00, 0x00, 0x05, 0x01 }; if (!i->init ()) { delete i; return 0; } do { cnt++; i->Send_Packet (CArray (ask, sizeof (ask))); do { r = i->Get_Packet (0); if (r) { r1 = *r; if (r1 () != 64) goto cont; if (r1[0] != 01) goto cont; if ((r1[1] & 0x0f) != 0x03) goto cont; if (r1[2] != 0x0b) goto cont; if (r1[3] != 0x00) goto cont; if (r1[4] != 0x08) goto cont; if (r1[5] != 0x00) goto cont; if (r1[6] != 0x03) goto cont; if (r1[7] != 0x0F) goto cont; if (r1[8] != 0x02) goto cont; if (r1[11] != 0x01) goto cont; break; cont: delete r; r = 0; } } while (!r); delete r; if (r1[13] & 0x2) emiver = 2; else if (r1[13] & 0x1) emiver = 1; else if (r1[13] & 0x4) emiver = 3; else emiver = 0; if (emiver == 0 && cnt < 5) pth_sleep (5); } while (emiver == 0 && cnt < 5); switch (emiver) { case 1: init[12] = 1; i->Send_Packet (CArray (init, sizeof (init))); iface = new USBConverterInterface (i, tr, LowLevelDriver::vEMI1); break; case 2: init[12] = 2; i->Send_Packet (CArray (init, sizeof (init))); iface = new USBConverterInterface (i, tr, LowLevelDriver::vEMI2); break; case 3: init[12] = 3; i->Send_Packet (CArray (init, sizeof (init))); iface = new USBConverterInterface (i, tr, LowLevelDriver::vCEMI); break; default: TRACEPRINTF (tr, 1, i, "Unsupported EMI %02x %02x", r1[12], r1[13]); delete i; return 0; } return iface; }
template <class type> void Vector<type>::Sort() { std::sort(CArray(), CArray() + Length()); }
template <class type> void Vector<type>::ZeroMem() { memset(CArray(), 0, Length() * sizeof(type)); }
template <class type> UINT64 Vector<type>::Hash64() const { return Utility::Hash64((const BYTE *)CArray(), sizeof(type) * Length()); }