static VALUE bdb_s_log_put_internal(VALUE obj, VALUE a, int flag) { bdb_ENV *envst; VALUE ret; DBT data; struct dblsnst *lsnst; GetEnvDB(obj, envst); if (TYPE(a) != T_STRING) a = rb_str_to_str(a); ret = bdb_makelsn(obj); Data_Get_Struct(ret, struct dblsnst, lsnst); data.data = StringValuePtr(a); data.size = RSTRING_LEN(a); #if HAVE_ST_DB_ENV_LG_INFO if (!envst->envp->lg_info) { rb_raise(bdb_eFatal, "log region not open"); } bdb_test_error(log_put(envst->envp->lg_info, lsnst->lsn, &data, flag)); #elif HAVE_ST_DB_ENV_LOG_PUT bdb_test_error(envst->envp->log_put(envst->envp, lsnst->lsn, &data, flag)); #else bdb_test_error(log_put(envst->envp, lsnst->lsn, &data, flag)); #endif return ret; }
void uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { struct serial_port_windows *spw; log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Serial port speed requested to be set to %d bauds.", uiPortSpeed); // Set port speed (Input and Output) switch (uiPortSpeed) { case 9600: case 19200: case 38400: case 57600: case 115200: case 230400: case 460800: break; default: log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of these constants: 9600 (default), 19200, 38400, 57600, 115200, 230400 or 460800.", uiPortSpeed); return; }; spw = (struct serial_port_windows *) sp; // Set baud rate spw->dcb.BaudRate = uiPortSpeed; if (!SetCommState(spw->hPort, &spw->dcb)) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to apply new speed settings."); return; } PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR); }
static int pn532_uart_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { int res = 0; // Before sending anything, we need to discard from any junk bytes uart_flush_input(DRIVER_DATA(pnd)->port); switch (CHIP_DATA(pnd)->power_mode) { case LOWVBAT: { /** PN532C106 wakeup. */ if ((res = pn532_uart_wakeup(pnd)) < 0) { return res; } // According to PN532 application note, C106 appendix: to go out Low Vbat mode and enter in normal mode we need to send a SAMConfiguration command if ((res = pn532_SAMConfiguration(pnd, PSM_NORMAL, 1000)) < 0) { return res; } } break; case POWERDOWN: { if ((res = pn532_uart_wakeup(pnd)) < 0) { return res; } } break; case NORMAL: // Nothing to do :) break; }; uint8_t abtFrame[PN532_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; if ((res = pn53x_build_frame(abtFrame, &szFrame, pbtData, szData)) < 0) { pnd->last_error = res; return pnd->last_error; } res = uart_send(DRIVER_DATA(pnd)->port, abtFrame, szFrame, timeout); if (res != 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->last_error = res; return pnd->last_error; } uint8_t abtRxBuf[6]; res = uart_receive(DRIVER_DATA(pnd)->port, abtRxBuf, 6, 0, timeout); if (res != 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Unable to read ACK"); pnd->last_error = res; return pnd->last_error; } if (pn53x_check_ack_frame(pnd, abtRxBuf, sizeof(abtRxBuf)) == 0) { // The PN53x is running the sent command } else { return pnd->last_error; } return NFC_SUCCESS; }
void write_enum(int outfd, sectionrec* sref) { hashlist* h; hashlist_iterator iter2; hashrec *href; stringptrlist* newlist; size_t i; stringptr *entry, normalized; char norm_buf[1024]; char buf[1024]; log_put(outfd, VARISL("typedef enum {\n\tstringswitch_enumerator_default_member_name("), VARIS(sref->id), VARISL("),"), NULL); h = sref->h; hashlist_iterator_init(&iter2); while((href = hashlist_next(h, &iter2))) { newlist = href->list; for(i = 0; i < stringptrlist_getsize(newlist); i++) { entry = stringptrlist_get(newlist, i); log_put(1, VARISL("id "), VARIS(sref->id), VARISL(", entry: "), VARII((int) entry->size), VARISL(", "), VARIS(entry), NULL); normalized = normalize(entry, norm_buf, sizeof(norm_buf)); ulz_snprintf(buf, sizeof(buf), "\tstringswitch_enumerator_member_name(%s, %s),\n", sref->id->ptr, normalized.ptr); log_putc(outfd, buf); } } log_put(outfd, VARISL("} stringswitch_enumerator_name("), VARIS(sref->id), VARISL(");\n"), NULL); }
void arygon_firmware(nfc_device *pnd, char *str) { const uint8_t arygon_firmware_version_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'v' }; uint8_t abtRx[16]; size_t szRx = sizeof(abtRx); int res = uart_send(DRIVER_DATA(pnd)->port, arygon_firmware_version_cmd, sizeof(arygon_firmware_version_cmd), 0); if (res != 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Unable to send ARYGON firmware command."); return; } res = uart_receive(DRIVER_DATA(pnd)->port, abtRx, szRx, 0, 0); if (res != 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Unable to retrieve ARYGON firmware version."); return; } if (0 == memcmp(abtRx, arygon_error_none, 6)) { uint8_t *p = abtRx + 6; unsigned int szData; sscanf((const char *)p, "%02x%9s", &szData, p); if (szData > 9) szData = 9; memcpy(str, p, szData); *(str + szData) = '\0'; } }
static void conf_parse_file(const char *filename, void (*conf_keyvalue)(void *data, const char *key, const char *value), void *data) { FILE *f = fopen(filename, "r"); if (!f) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Unable to open file: %s", filename); return; } char line[BUFSIZ]; int lineno = 0; while (fgets(line, BUFSIZ, f) != NULL) { lineno++; switch (line[0]) { case '#': case '\n': break; default: { char *key; char *value; if (parse_line(line, &key, &value) == 0) { conf_keyvalue(data, key, value); free(key); free(value); } else { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Parse error on line #%d: %s", lineno, line); } } } } fclose(f); return; }
nfc_device * acr122_open (const nfc_connstring connstring) { struct acr122_descriptor ndd; int connstring_decode_level = acr122_connstring_decode (connstring, &ndd); if (connstring_decode_level < 2) { return NULL; } // FIXME: acr122_open() does not take care about bus index char *pcFirmware; nfc_device *pnd = nfc_device_new (connstring); pnd->driver_data = malloc (sizeof (struct acr122_data)); // Alloc and init chip's data pn53x_data_new (pnd, &acr122_io); SCARDCONTEXT *pscc; log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to open %s", ndd.pcsc_device_name); // Test if context succeeded if (!(pscc = acr122_get_scardcontext ())) goto error; // Test if we were able to connect to the "emulator" card if (SCardConnect (*pscc, ndd.pcsc_device_name, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &(DRIVER_DATA (pnd)->hCard), (void *) &(DRIVER_DATA (pnd)->ioCard.dwProtocol)) != SCARD_S_SUCCESS) { // Connect to ACR122 firmware version >2.0 if (SCardConnect (*pscc, ndd.pcsc_device_name, SCARD_SHARE_DIRECT, 0, &(DRIVER_DATA (pnd)->hCard), (void *) &(DRIVER_DATA (pnd)->ioCard.dwProtocol)) != SCARD_S_SUCCESS) { // We can not connect to this device. log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PCSC connect failed"); goto error; } } // Configure I/O settings for card communication DRIVER_DATA (pnd)->ioCard.cbPciLength = sizeof (SCARD_IO_REQUEST); // Retrieve the current firmware version pcFirmware = acr122_firmware (pnd); if (strstr (pcFirmware, FIRMWARE_TEXT) != NULL) { // Done, we found the reader we are looking for snprintf (pnd->name, sizeof (pnd->name), "%s / %s", ndd.pcsc_device_name, pcFirmware); // 50: empirical tuning on Touchatag // 46: empirical tuning on ACR122U CHIP_DATA (pnd)->timer_correction = 50; pnd->driver = &acr122_driver; pn53x_init (pnd); return pnd; } error: nfc_device_free (pnd); return NULL; }
int acr122_usb_init(nfc_device *pnd) { int res = 0; int i; uint8_t abtRxBuf[255 + sizeof(struct ccid_header)]; /* // See ACR122 manual: "Bi-Color LED and Buzzer Control" section uint8_t acr122u_get_led_state_frame[] = { 0x6b, // CCID 0x09, // lenght of frame 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // padding // frame: 0xff, // Class 0x00, // INS 0x40, // P1: Get LED state command 0x00, // P2: LED state control 0x04, // Lc 0x00, 0x00, 0x00, 0x00, // Blinking duration control }; log_put (LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "ACR122 Get LED state"); if ((res = acr122_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *) acr122u_get_led_state_frame, sizeof (acr122u_get_led_state_frame), 1000)) < 0) return res; if ((res = acr122_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), 1000)) < 0) return res; */ if ((res = pn53x_set_property_int(pnd, NP_TIMEOUT_COMMAND, 1000)) < 0) return res; // Power On ICC uint8_t ccid_frame[] = { PC_to_RDR_IccPowerOn, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 }; if ((res = acr122_usb_bulk_write(DRIVER_DATA(pnd), ccid_frame, sizeof(struct ccid_header), 1000)) < 0) return res; if ((res = acr122_usb_bulk_read(DRIVER_DATA(pnd), abtRxBuf, sizeof(abtRxBuf), 1000)) < 0) return res; log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "ACR122 PICC Operating Parameters"); if ((res = acr122_usb_send_apdu(pnd, 0x00, 0x51, 0x00, NULL, 0, 0, abtRxBuf, sizeof(abtRxBuf))) < 0) return res; res = 0; for (i = 0; i < 3; i++) { if (res < 0) log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "PN532 init failed, trying again..."); if ((res = pn53x_init(pnd)) >= 0) break; } if (res < 0) return res; return NFC_SUCCESS; }
static bool conf_parse_file(const char *filename, void (*conf_keyvalue)(void *data, const char *key, const char *value), void *data) { FILE *f = fopen(filename, "r"); if (!f) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Unable to open file: %s", filename); return false; } char line[BUFSIZ]; const char *str_regex = "^[[:space:]]*([[:alnum:]_.]+)[[:space:]]*=[[:space:]]*(\"(.+)\"|([^[:space:]]+))[[:space:]]*$"; regex_t preg; if (regcomp(&preg, str_regex, REG_EXTENDED | REG_NOTEOL) != 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Regular expression used for configuration file parsing is not valid."); fclose(f); return false; } size_t nmatch = preg.re_nsub + 1; regmatch_t *pmatch = malloc(sizeof(*pmatch) * nmatch); if (!pmatch) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Not enough memory: malloc failed."); regfree(&preg); fclose(f); return false; } int lineno = 0; while (fgets(line, BUFSIZ, f) != NULL) { lineno++; switch (line[0]) { case '#': case '\n': break; default: { int match; if ((match = regexec(&preg, line, nmatch, pmatch, 0)) == 0) { const size_t key_size = pmatch[1].rm_eo - pmatch[1].rm_so; const off_t value_pmatch = pmatch[3].rm_eo != -1 ? 3 : 4; const size_t value_size = pmatch[value_pmatch].rm_eo - pmatch[value_pmatch].rm_so; char key[key_size + 1]; char value[value_size + 1]; strncpy(key, line + (pmatch[1].rm_so), key_size); key[key_size] = '\0'; strncpy(value, line + (pmatch[value_pmatch].rm_so), value_size); value[value_size] = '\0'; conf_keyvalue(data, key, value); } else { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Parse error on line #%d: %s", lineno, line); } } break; } } free(pmatch); regfree(&preg); fclose(f); return false; }
int pn53x_usb_init (nfc_device *pnd) { int res = 0; // Sometimes PN53x USB doesn't reply ACK one the first frame, so we need to send a dummy one... //pn53x_check_communication (pnd); // Sony RC-S360 doesn't support this command for now so let's use a get_firmware_version instead: const uint8_t abtCmd[] = { GetFirmwareVersion }; pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, 0); // ...and we don't care about error pnd->last_error = 0; if (SONY_RCS360 == DRIVER_DATA (pnd)->model) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "SONY RC-S360 initialization."); const uint8_t abtCmd2[] = { 0x18, 0x01 }; pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, 0, 0); pn53x_usb_ack (pnd); } if ((res = pn53x_init (pnd)) < 0) return res; if (ASK_LOGO == DRIVER_DATA (pnd)->model) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "ASK LoGO initialization."); /* Internal registers */ /* Disable 100mA current limit, Power on Secure IC (SVDD) */ pn53x_write_register (pnd, PN53X_REG_Control_switch_rng, 0xFF, SYMBOL_CURLIMOFF | SYMBOL_SIC_SWITCH_EN | SYMBOL_RANDOM_DATAREADY); /* Select the signal to be output on SIGOUT: Modulation signal (envelope) from the internal coder */ pn53x_write_register (pnd, PN53X_REG_CIU_TxSel, 0xFF, 0x14); /* SFR Registers */ /* Setup push-pulls for pins from P30 to P35 */ pn53x_write_register (pnd, PN53X_SFR_P3CFGB, 0xFF, 0x37); /* On ASK LoGO hardware: LEDs port bits definition: * LED 1: bit 2 (P32) * LED 2: bit 1 (P31) * LED 3: bit 0 or 3 (depending of hardware revision) (P30 or P33) * LED 4: bit 5 (P35) Notes: * Set logical 0 to switch LED on; logical 1 to switch LED off. * Bit 4 should be maintained at 1 to keep RF field on. Progressive field activation: The ASK LoGO hardware can progressively power-up the antenna. To use this feature we have to switch on the field by switching on the field on PN533 (RFConfiguration) then set P34 to '1', and cut-off the field by switching off the field on PN533 then set P34 to '0'. */ /* Set P30, P31, P33, P35 to logic 1 and P32, P34 to 0 logic */ /* ie. Switch LED1 on and turn off progressive field */ pn53x_write_register (pnd, PN53X_SFR_P3, 0xFF, _BV (P30) | _BV (P31) | _BV (P33) | _BV (P35)); } return NFC_SUCCESS; }
static size_t pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { (void)context; usb_prepare(); size_t device_found = 0; uint32_t uiBusIndex = 0; struct usb_bus *bus; for (bus = usb_get_busses(); bus; bus = bus->next) { struct usb_device *dev; for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) { for (size_t n = 0; n < sizeof(pn53x_usb_supported_devices) / sizeof(struct pn53x_usb_supported_device); n++) { if ((pn53x_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) && (pn53x_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) { // Make sure there are 2 endpoints available // with libusb-win32 we got some null pointers so be robust before looking at endpoints: if (dev->config == NULL || dev->config->interface == NULL || dev->config->interface->altsetting == NULL) { // Nope, we maybe want the next one, let's try to find another continue; } if (dev->config->interface->altsetting->bNumEndpoints < 2) { // Nope, we maybe want the next one, let's try to find another continue; } usb_dev_handle *udev = usb_open(dev); if (udev == NULL) continue; // Set configuration int res = usb_set_configuration(udev, 1); if (res < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); usb_close(udev); // we failed to use the device continue; } // pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s", bus->dirname, dev->filename); usb_close(udev); snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename); device_found++; // Test if we reach the maximum "wanted" devices if (device_found == connstrings_len) { return device_found; } } } } } return device_found; }
int uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout) { DWORD dwBytesToGet = (DWORD)szRx; DWORD dwBytesReceived = 0; DWORD dwTotalBytesReceived = 0; BOOL res; // XXX Put this part into uart_win32_timeouts () ? DWORD timeout_ms = timeout; COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = 0; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = timeout_ms; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = timeout_ms; if (!SetCommTimeouts(((struct serial_port_windows *) sp)->hPort, &timeouts)) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to apply new timeout settings."); return NFC_EIO; } log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Timeouts are set to %lu ms", timeout_ms); // TODO Enhance the reception method // - According to MSDN, it could be better to implement nfc_abort_command() mecanism using Cancello() volatile bool *abort_flag_p = (volatile bool *)abort_p; do { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "ReadFile"); res = ReadFile(((struct serial_port_windows *) sp)->hPort, pbtRx + dwTotalBytesReceived, dwBytesToGet, &dwBytesReceived, NULL); dwTotalBytesReceived += dwBytesReceived; if (!res) { DWORD err = GetLastError(); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "ReadFile error: %lu", err); return NFC_EIO; } else if (dwBytesReceived == 0) { return NFC_ETIMEOUT; } if (((DWORD)szRx) > dwTotalBytesReceived) { dwBytesToGet -= dwBytesReceived; } if (abort_flag_p != NULL && (*abort_flag_p) && dwTotalBytesReceived == 0) { return NFC_EOPABORTED; } } while (((DWORD)szRx) > dwTotalBytesReceived); LOG_HEX(LOG_GROUP, "RX", pbtRx, szRx); return (dwTotalBytesReceived == (DWORD) szRx) ? 0 : NFC_EIO; }
/** * @brief List opened devices * * Probe PCSC to find NFC capable hardware. * * @param pnddDevices Array of nfc_device_desc_t previously allocated by the caller. * @param szDevices size of the pnddDevices array. * @param pszDeviceFound number of devices found. * @return true if succeeded, false otherwise. */ bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) { size_t szPos = 0; char acDeviceNames[256 + 64 * PCSC_MAX_DEVICES]; size_t szDeviceNamesLen = sizeof (acDeviceNames); uint32_t uiBusIndex = 0; SCARDCONTEXT *pscc; bool bSupported; int i; // Clear the reader list memset (acDeviceNames, '\0', szDeviceNamesLen); *pszDeviceFound = 0; // Test if context succeeded if (!(pscc = acr122_get_scardcontext ())) { log_put (LOG_CATEGORY, NFC_PRIORITY_WARN, "%s", "PCSC context not found (make sure PCSC daemon is running)."); return false; } // Retrieve the string array of all available pcsc readers DWORD dwDeviceNamesLen = szDeviceNamesLen; if (SCardListReaders (*pscc, NULL, acDeviceNames, &dwDeviceNamesLen) != SCARD_S_SUCCESS) return false; while ((acDeviceNames[szPos] != '\0') && ((*pszDeviceFound) < connstrings_len)) { uiBusIndex++; // DBG("- %s (pos=%ld)", acDeviceNames + szPos, (unsigned long) szPos); bSupported = false; for (i = 0; supported_devices[i] && !bSupported; i++) { int l = strlen (supported_devices[i]); bSupported = 0 == strncmp (supported_devices[i], acDeviceNames + szPos, l); } if (bSupported) { // Supported ACR122 device found snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%"PRIu32, ACR122_DRIVER_NAME, acDeviceNames + szPos, uiBusIndex); (*pszDeviceFound)++; } else { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos); } // Find next device name position while (acDeviceNames[szPos++] != '\0'); } acr122_free_scardcontext (); return true; }
void uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Serial port speed requested to be set to %d bauds.", uiPortSpeed); // Portability note: on some systems, B9600 != 9600 so we have to do // uint32_t <=> speed_t associations by hand. speed_t stPortSpeed = B9600; switch (uiPortSpeed) { case 9600: stPortSpeed = B9600; break; case 19200: stPortSpeed = B19200; break; case 38400: stPortSpeed = B38400; break; # ifdef B57600 case 57600: stPortSpeed = B57600; break; # endif # ifdef B115200 case 115200: stPortSpeed = B115200; break; # endif # ifdef B230400 case 230400: stPortSpeed = B230400; break; # endif # ifdef B460800 case 460800: stPortSpeed = B460800; break; # endif default: log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of those defined in termios(3).", uiPortSpeed); return; }; // Set port speed (Input and Output) cfsetispeed(&(UART_DATA(sp)->termios_new), stPortSpeed); cfsetospeed(&(UART_DATA(sp)->termios_new), stPortSpeed); if (tcsetattr(UART_DATA(sp)->fd, TCSADRAIN, &(UART_DATA(sp)->termios_new)) == -1) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to apply new speed settings."); } }
static int arygon_tama_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { int res = 0; // Before sending anything, we need to discard from any junk bytes uart_flush_input(DRIVER_DATA(pnd)->port, false); uint8_t abtFrame[ARYGON_TX_BUFFER_LEN] = { DEV_ARYGON_PROTOCOL_TAMA, 0x00, 0x00, 0xff }; // Every packet must start with "0x32 0x00 0x00 0xff" size_t szFrame = 0; if (szData > PN53x_NORMAL_FRAME__DATA_MAX_LEN) { // ARYGON Reader with PN532 equipped does not support extended frame (bug in ARYGON firmware?) log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "ARYGON device does not support more than %d bytes as payload (requested: %" PRIdPTR ")", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData); pnd->last_error = NFC_EDEVNOTSUPP; return pnd->last_error; } if ((res = pn53x_build_frame(abtFrame + 1, &szFrame, pbtData, szData)) < 0) { pnd->last_error = res; return pnd->last_error; } if ((res = uart_send(DRIVER_DATA(pnd)->port, abtFrame, szFrame + 1, timeout)) != 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->last_error = res; return pnd->last_error; } uint8_t abtRxBuf[PN53x_ACK_FRAME__LEN]; if ((res = uart_receive(DRIVER_DATA(pnd)->port, abtRxBuf, sizeof(abtRxBuf), 0, timeout)) != 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to read ACK"); pnd->last_error = res; return pnd->last_error; } if (pn53x_check_ack_frame(pnd, abtRxBuf, sizeof(abtRxBuf)) == 0) { // The PN53x is running the sent command } else if (0 == memcmp(arygon_error_unknown_mode, abtRxBuf, sizeof(abtRxBuf))) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Bad frame format."); // We have already read 6 bytes and arygon_error_unknown_mode is 10 bytes long // so we have to read 4 remaining bytes to be synchronized at the next receiving pass. pnd->last_error = uart_receive(DRIVER_DATA(pnd)->port, abtRxBuf, 4, 0, timeout); return pnd->last_error; } else { return pnd->last_error; } return NFC_SUCCESS; }
int uart_send(serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout) { DWORD dwTxLen = 0; COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = 0; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = timeout; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = timeout; if (!SetCommTimeouts(((struct serial_port_windows *) sp)->hPort, &timeouts)) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to apply new timeout settings."); return NFC_EIO; } LOG_HEX(LOG_GROUP, "TX", pbtTx, szTx); if (!WriteFile(((struct serial_port_windows *) sp)->hPort, pbtTx, szTx, &dwTxLen, NULL)) { return NFC_EIO; } if (!dwTxLen) return NFC_EIO; return 0; }
void launch_thread(jobtype ptype, pkgstate* state, pkg_exec* item, pkgdata* data) { char* arr[2]; create_script(ptype, state, item, data); log_timestamp(1); log_putspace(1); if(ptype == JT_DOWNLOAD) { log_puts(1, SPL("downloading ")); } else log_puts(1, SPL("building ")); log_put(1, VARIS(item->name), VARISL("("), VARIS(item->scripts.filename), VARISL(") -> "), VARIS(item->scripts.stdoutfn), NULL); arr[0] = item->scripts.filename->ptr; arr[1] = NULL; posix_spawn_file_actions_init(&item->fa); posix_spawn_file_actions_addclose(&item->fa, 0); posix_spawn_file_actions_addclose(&item->fa, 1); posix_spawn_file_actions_addclose(&item->fa, 2); posix_spawn_file_actions_addopen(&item->fa, 0, "/dev/null", O_RDONLY, 0); posix_spawn_file_actions_addopen(&item->fa, 1, item->scripts.stdoutfn->ptr, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); posix_spawn_file_actions_adddup2(&item->fa, 1, 2); int ret = posix_spawnp(&item->pid, arr[0], &item->fa, NULL, arr, environ); if(ret == -1) { log_perror("posix_spawn"); die(SPL("")); } }
int arygon_reset_tama(nfc_device *pnd) { const uint8_t arygon_reset_tama_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'r' }; uint8_t abtRx[10]; // Attempted response is 10 bytes long size_t szRx = sizeof(abtRx); int res; uart_send(DRIVER_DATA(pnd)->port, arygon_reset_tama_cmd, sizeof(arygon_reset_tama_cmd), 500); // Two reply are possible from ARYGON device: arygon_error_none (ie. in case the byte is well-sent) // or arygon_error_unknown_mode (ie. in case of the first byte was bad-transmitted) res = uart_receive(DRIVER_DATA(pnd)->port, abtRx, szRx, 0, 1000); if (res != 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "No reply to 'reset TAMA' command."); pnd->last_error = res; return pnd->last_error; } if (0 != memcmp(abtRx, arygon_error_none, sizeof(arygon_error_none) - 1)) { pnd->last_error = NFC_EIO; return pnd->last_error; } return NFC_SUCCESS; }
int pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable) { int res = 0; if ((res = pn53x_set_property_bool (pnd, property, bEnable)) < 0) return res; switch (DRIVER_DATA (pnd)->model) { case ASK_LOGO: if (NP_ACTIVATE_FIELD == property) { /* Switch on/off LED2 and Progressive Field GPIO according to ACTIVATE_FIELD option */ log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Switch progressive field %s", bEnable ? "On" : "Off"); if ((res = pn53x_write_register (pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV (P34) : _BV (P31))) < 0) return NFC_ECHIP; } break; case SCM_SCL3711: if (NP_ACTIVATE_FIELD == property) { // Switch on/off LED according to ACTIVATE_FIELD option if ((res = pn53x_write_register (pnd, PN53X_SFR_P3, _BV (P32), bEnable ? 0 : _BV (P32))) < 0) return res; } break; default: break; } return NFC_SUCCESS; }
static void conf_devices_load(const char *dirname, nfc_context *context) { DIR *d = opendir(dirname); if (!d) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Unable to open directory: %s", dirname); } else { struct dirent *de; while ((de = readdir(d)) != NULL) { // FIXME add a way to sort devices if (de->d_name[0] != '.') { const size_t filename_len = strlen(de->d_name); const size_t extension_len = strlen(".conf"); if ((filename_len > extension_len) && (strncmp(".conf", de->d_name + (filename_len - extension_len), extension_len) == 0)) { char filename[BUFSIZ] = LIBNFC_DEVICECONFDIR"/"; strcat(filename, de->d_name); struct stat s; if (stat(filename, &s) == -1) { perror("stat"); continue; } if (S_ISREG(s.st_mode)) { conf_parse_file(filename, conf_keyvalue_device, context); } } } } closedir(d); } }
void dump_variants(int outfd, stringptr* section_name, variant_info_list* variants, stringptrlist* block_strings, size_t level, size_t previous_membercount, ssize_t backindent) { if(variants == NULL) return; variant_info* vp; stringptr* act, normalized; char norm_buf[1024]; size_t membercount; size_t* id; int switched = 0; sblist_iter(variants, vp) { membercount = sblist_getsize(vp->members); if(membercount == previous_membercount) { print_level_tabs(outfd, level - backindent); ulz_fprintf(outfd, "if(str[%zu]!='%c') goto main_default;\n", level, vp->c); backindent++; } else { if(!switched) { print_level_tabs(outfd, level - backindent); ulz_fprintf(outfd, "switch(str[%zu]) {\n", level); switched = 1; backindent--; } print_level_tabs(outfd, level - backindent); ulz_fprintf(outfd, "case '%c':\n", vp->c); } if(!vp->variants) { print_level_tabs(outfd, level - backindent); id = sblist_get(vp->members, 0); act = sblist_get(block_strings, *id); normalized = normalize(act, norm_buf, sizeof(norm_buf)); log_put(outfd, VARISL("\treturn stringswitch_enumerator_member_name("), VARIS(section_name), VARISL(", "), VARIS(&normalized), VARISL(");"), NULL); } dump_variants(outfd, section_name, vp->variants, block_strings, level+1, membercount, backindent); }
void start_log(void) { if(!LOG_FILE) { char path[500]; char* ch; GetModuleFileName(NULL, path, MAX_PATH); ch = strrchr(path, '\\'); if ( ch ) *(ch+1) = 0; strcat(path, "rohos_dv_ntfs.log"); LOG_FILE = INVALID_HANDLE_VALUE; if ( _access(path, 0) ==0 ) LOG_FILE = CreateFileA(path,GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL); //if(LOG_FILE==(HANDLE)INVALID_HANDLE_VALUE) // LOG_FILE = NULL; if (LOG_FILE != INVALID_HANDLE_VALUE) { SetFilePointer(LOG_FILE,0,0,FILE_END); log_put("LOG_STARTED----------------------------"); } } }
nfc_context * nfc_context_new(void) { nfc_context *res = malloc(sizeof(*res)); if (!res) { return NULL; } #ifdef DEBUG res->log_level = 3; #else res->log_level = 1; #endif // Clear user defined devices array strcpy(res->user_defined_devices.name, ""); strcpy(res->user_defined_devices.connstring, ""); res->user_defined_devices.optional = 0; res->user_defined_device_count = 0; #ifdef LOG // Initialize log before use it... log_init(res); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d device(s) defined by user", res->user_defined_device_count); #endif return res; }
/** * @brief List opened devices * * Probe PCSC to find ACR122 devices (ACR122U and Touchatag/Tikitag). * * @param connstring array of nfc_connstring where found device's connection strings will be stored. * @param connstrings_len size of connstrings array. * @return number of devices found. */ static size_t acr122_pcsc_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { (void) context; size_t szPos = 0; char acDeviceNames[256 + 64 * PCSC_MAX_DEVICES]; size_t szDeviceNamesLen = sizeof(acDeviceNames); SCARDCONTEXT *pscc; int i; // Clear the reader list memset(acDeviceNames, '\0', szDeviceNamesLen); // Test if context succeeded if (!(pscc = acr122_pcsc_get_scardcontext())) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Warning: %s", "PCSC context not found (make sure PCSC daemon is running)."); return 0; } // Retrieve the string array of all available pcsc readers DWORD dwDeviceNamesLen = szDeviceNamesLen; if (SCardListReaders(*pscc, NULL, acDeviceNames, &dwDeviceNamesLen) != SCARD_S_SUCCESS) return 0; size_t device_found = 0; while ((acDeviceNames[szPos] != '\0') && (device_found < connstrings_len)) { bool bSupported = false; for (i = 0; supported_devices[i] && !bSupported; i++) { int l = strlen(supported_devices[i]); bSupported = 0 == strncmp(supported_devices[i], acDeviceNames + szPos, l); } if (bSupported) { // Supported ACR122 device found snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s", ACR122_PCSC_DRIVER_NAME, acDeviceNames + szPos); device_found++; } else { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos); } // Find next device name position while (acDeviceNames[szPos++] != '\0'); } acr122_pcsc_free_scardcontext(); return device_found; }
//return 0 on success. //checks if filesize and/or sha512 matches, if used. int verify_tarball(pkgconfig* cfg, pkgdata* package) { char buf[4096]; char* error; SHA512_CTX ctx; int fd; uint64_t pos, len = 0, nread; stringptr hash; get_tarball_filename_with_path(cfg, package, buf, sizeof(buf)); if(package->filesize) { len = getfilesize(buf); if(len < package->filesize) { log_put(2, VARISL("WARNING: "), VARIC(buf), VARISL(" filesize too small!"), NULL); return 1; } else if (len > package->filesize) { log_put(2, VARISL("WARNING: "), VARIC(buf), VARISL(" filesize too big!"), NULL); return 2; } } if(package->sha512) { if(!len) len = getfilesize(buf); fd = open(buf, O_RDONLY); if(fd == -1) { error = strerror(errno); log_put(2, VARISL("WARNING: "), VARIC(buf), VARISL(" failed to open: "), VARIC(error), NULL); return 3; } SHA512_Init(&ctx); pos = 0; while(pos < len) { nread = read(fd, buf, sizeof(buf)); SHA512_Update(&ctx, (const uint8_t*) buf, nread); pos += nread; } close(fd); SHA512_End(&ctx, (char*) buf); hash.ptr = buf; hash.size = strlen(buf); if(!EQ(&hash, package->sha512)) { log_put(2, VARISL("WARNING: "), VARIS(package->name), VARISL(" sha512 mismatch, got "), VARIS(&hash), VARISL(", expected "), VARIS(package->sha512), NULL); return 4; } } return 0; }
static void conf_keyvalue_context(void *data, const char *key, const char *value) { nfc_context *context = (nfc_context *)data; log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "key: [%s], value: [%s]", key, value); if (strcmp(key, "allow_autoscan") == 0) { string_as_boolean(value, &(context->allow_autoscan)); } else if (strcmp(key, "allow_intrusive_scan") == 0) { string_as_boolean(value, &(context->allow_intrusive_scan)); } else if (strcmp(key, "log_level") == 0) { context->log_level = atoi(value); } else if (strcmp(key, "device.name") == 0) { if ((context->user_defined_device_count == 0) || strcmp(context->user_defined_devices[context->user_defined_device_count - 1].name, "") != 0) { if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Configuration exceeded maximum user-defined devices."); return; } context->user_defined_device_count++; } strncpy(context->user_defined_devices[context->user_defined_device_count - 1].name, value, DEVICE_NAME_LENGTH - 1); context->user_defined_devices[context->user_defined_device_count - 1].name[DEVICE_NAME_LENGTH - 1] = '\0'; } else if (strcmp(key, "device.connstring") == 0) { if ((context->user_defined_device_count == 0) || strcmp(context->user_defined_devices[context->user_defined_device_count - 1].connstring, "") != 0) { if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Configuration exceeded maximum user-defined devices."); return; } context->user_defined_device_count++; } strncpy(context->user_defined_devices[context->user_defined_device_count - 1].connstring, value, NFC_BUFSIZE_CONNSTRING - 1); context->user_defined_devices[context->user_defined_device_count - 1].connstring[NFC_BUFSIZE_CONNSTRING - 1] = '\0'; } else if (strcmp(key, "device.optional") == 0) { if ((context->user_defined_device_count == 0) || context->user_defined_devices[context->user_defined_device_count - 1].optional) { if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Configuration exceeded maximum user-defined devices."); return; } context->user_defined_device_count++; } if ((strcmp(value, "true") == 0) || (strcmp(value, "True") == 0) || (strcmp(value, "1") == 0)) //optional context->user_defined_devices[context->user_defined_device_count - 1].optional = true; } else { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Unknown key in config line: %s = %s", key, value); } }
/** * @brief Write a frame to I2C device containing \a pbtTx content * * @param id I2C device. * @param pbtTx pointer on buffer containing data * @param szTx length of the buffer * @return NFC_SUCCESS on success, otherwise driver error code */ int i2c_write(i2c_device id, const uint8_t *pbtTx, const size_t szTx) { LOG_HEX(LOG_GROUP, "TX", pbtTx, szTx); ssize_t writeCount; writeCount = write(I2C_DATA(id) ->fd, pbtTx, szTx); if ((const ssize_t) szTx == writeCount) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "wrote %d bytes successfully.", (int)szTx); return NFC_SUCCESS; } else { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Error: wrote only %d bytes (%d expected) (%s).", (int)writeCount, (int) szTx, strerror(errno)); return NFC_EIO; } }
int rc522_uart_reset_baud_rate(struct nfc_device * pnd) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Restoring baud rate to default of %d bps.", BOOT_BAUD_RATE); /* MODIF !!! return uart_set_speed(DRIVER_DATA(pnd)->port, BOOT_BAUD_RATE); */ uart_set_speed(DRIVER_DATA(pnd)->port, BOOT_BAUD_RATE); return 0; }
void print_queue(pkgstate* state, jobtype jt) { sblist* queue = (jt == JT_DOWNLOAD) ? state->dl_queue : state->build_queue; char *queuename = (jt == JT_DOWNLOAD) ? "download" : "build"; pkg_exec* listitem; log_put(1, VARISL("*** "), VARIC(queuename), VARISL("queue ***"), NULL); sblist_iter(queue, listitem) { log_puts(1, listitem->name); log_putln(1); }
int WriteLog2(char* format, ... ) { //if (!LOG_FILE) log_put(format); //MessageBox(0, format, "ntfs", 0); //WriteLog(format); }