#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hidapi.h"
#include "atca_hal.h"
#include "hal_all_platforms_kit_hidapi.h"
#include "hal/kit_protocol.h"
atcahid_t _gHid;
ATCA_STATUS hal_kit_hid_discover_buses(int i2c_buses[], int max_buses)
{
return ATCA_UNIMPLEMENTED;
}
ATCA_STATUS hal_kit_hid_discover_devices(int bus_num, ATCAIfaceCfg cfg[], int *found)
{
return ATCA_UNIMPLEMENTED;
}
ATCA_STATUS hal_kit_hid_init(void* hal, ATCAIfaceCfg* cfg)
{
ATCAHAL_t *phal = (ATCAHAL_t*)hal;
hid_device *handle;
struct hid_device_info *devs = NULL;
struct hid_device_info *cur_dev = NULL;
int i = 0;
int index = 0;
if ((cfg == NULL) || (phal == NULL))
{
return ATCA_BAD_PARAM;
}
memset(&_gHid, 0, sizeof(_gHid));
for (i = 0; i < HID_DEVICES_MAX; i++)
{
_gHid.kits[i] = NULL;
}
_gHid.num_kits_found = 0;
#ifdef KIT_DEBUG
printf("Enumerate HID device(s)\n");
#endif
hid_init();
devs = hid_enumerate(cfg->atcahid.vid, cfg->atcahid.pid);
cur_dev = devs;
if (cur_dev == NULL)
{
#ifdef KIT_DEBUG
printf("no HID device found\n");
#endif
hid_exit();
return ATCA_COMM_FAIL;
}
while (cur_dev != NULL)
{
if ((handle = hid_open(cur_dev->vendor_id, cur_dev->product_id, cur_dev->serial_number)))
{
_gHid.kits[index] = handle;
#ifdef KIT_DEBUG
printf("Kit USB Device Node: %s\n", cur_dev->path);
printf(" Manufacturer %s (%s)\n",
(char*)cur_dev->manufacturer_string,
(char*)cur_dev->product_string);
printf(" PID %d\n", cur_dev->product_id);
printf(" VID %d\n\n", cur_dev->vendor_id);
#endif
index++;
}
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
if (index > 0)
{
_gHid.num_kits_found = index;
phal->hal_data = &_gHid;
}
return ATCA_SUCCESS;
}
ATCA_STATUS hal_kit_hid_post_init(ATCAIface iface)
{
ATCA_STATUS status = ATCA_SUCCESS;
atcahid_t* pHid = atgetifacehaldat(iface);
ATCAIfaceCfg *pCfg = atgetifacecfg(iface);
int i = 0;
if ((pHid == NULL) || (pCfg == NULL))
{
return ATCA_BAD_PARAM;
}
for (i = 0; i < pHid->num_kits_found; i++)
{
status = kit_init(iface);
if (status != ATCA_SUCCESS)
#ifdef KIT_DEBUG
{ printf("kit_init() Failed"); }
#endif
{ BREAK(status, "kit_init() failed"); }
}
return status;
}
ATCA_STATUS kit_phy_send(ATCAIface iface, uint8_t* txdata, int txlength)
{
ATCAIfaceCfg *cfg = atgetifacecfg(iface);
atcahid_t* pHid = (atcahid_t*)atgetifacehaldat(iface);
int bytes_written = 0;
int bytes_left;
int bytes_to_send;
uint8_t buffer[HID_PACKET_MAX];
if ((txdata == NULL) || (cfg == NULL) || (pHid == NULL))
{
return ATCA_BAD_PARAM;
}
if (pHid->kits[cfg->atcahid.idx] == NULL)
{
return ATCA_COMM_FAIL;
}
#ifdef KIT_DEBUG
printf("HID layer: Write: %s", txdata);
#endif
bytes_left = txlength;
while (bytes_left > 0)
{
memset(buffer, 0, (HID_PACKET_MAX));
if (bytes_left >= cfg->atcahid.packetsize)
{
bytes_to_send = cfg->atcahid.packetsize;
}
else
{
bytes_to_send = bytes_left;
}
memcpy(&buffer[1], &txdata[(txlength - bytes_left)], bytes_to_send);
bytes_written = hid_write(pHid->kits[cfg->atcahid.idx], buffer, cfg->atcahid.packetsize + 1);
if (bytes_written != cfg->atcahid.packetsize + 1)
{
return ATCA_TX_FAIL;
}
bytes_left -= bytes_to_send;
}
return ATCA_SUCCESS;
}
ATCA_STATUS kit_phy_receive(ATCAIface iface, uint8_t* rxdata, int* rxsize)
{
ATCAIfaceCfg *cfg = atgetifacecfg(iface);
atcahid_t* pHid = (atcahid_t*)atgetifacehaldat(iface);
bool continue_read = true;
size_t bytes_read = 0;
size_t total_bytes_read = 0;
size_t bytes_to_read = *rxsize;
char *location;
if ((rxdata == NULL) || (rxsize == NULL) || (cfg == NULL) || (pHid == NULL))
{
return ATCA_BAD_PARAM;
}
if (pHid->kits[cfg->atcahid.idx] == NULL)
{
return ATCA_COMM_FAIL;
}
bytes_to_read--;
do
{
bytes_read = hid_read(pHid->kits[cfg->atcahid.idx], &rxdata[total_bytes_read], bytes_to_read);
if (bytes_read == -1)
{
return ATCA_RX_FAIL;
}
total_bytes_read += bytes_read;
bytes_to_read -= bytes_read;
if (strstr((char*)rxdata, "\n") != NULL)
{
continue_read = false;
}
}
while (continue_read == true);
location = strchr((char*)rxdata, '\n');
if (location != NULL)
{
*rxsize = (int)(location - (char*)rxdata);
}
else
{
*rxsize = (int)total_bytes_read;
}
#ifdef KIT_DEBUG
printf("HID layer: Read: %s", rxdata);
#endif
return ATCA_SUCCESS;
}
ATCA_STATUS kit_phy_num_found(int8_t* num_found)
{
*num_found = _gHid.num_kits_found;
return ATCA_SUCCESS;
}
ATCA_STATUS hal_kit_hid_send(ATCAIface iface, uint8_t* txdata, int txlength)
{
return kit_send(iface, txdata, txlength);
}
ATCA_STATUS hal_kit_hid_receive(ATCAIface iface, uint8_t* rxdata, uint16_t* rxsize)
{
return kit_receive(iface, rxdata, rxsize);
}
ATCA_STATUS hal_kit_hid_wake(ATCAIface iface)
{
return kit_wake(iface);
}
ATCA_STATUS hal_kit_hid_idle(ATCAIface iface)
{
return kit_idle(iface);
}
ATCA_STATUS hal_kit_hid_sleep(ATCAIface iface)
{
return kit_sleep(iface);
}
ATCA_STATUS hal_kit_hid_release(void* hal_data)
{
atcahid_t* phaldat = (atcahid_t*)hal_data;
int i = 0;
if (phaldat == NULL)
{
return ATCA_BAD_PARAM;
}
for (i = 0; i < phaldat->num_kits_found; i++)
{
if (_gHid.kits[i] != NULL)
{
hid_close(_gHid.kits[i]);
}
}
return ATCA_SUCCESS;
}