/************************************************************
 **
 **     Object name : libkm-netapi.h
 **
 **     Description
 **     -- -- -- -- -- -- -- --
 **     RASPと同じネットワーク経由のインターフェースで
 **     利用するためのAPIのヘッダファイル
 **
 **     History
 **     -- -- -- -- -- -- -- --
 **     2013.09.01 M.Takigahira 新規作成
 **                             librasp-netapiをベースに移植
 **
 **     Copyright (c) 2013
 **     HRI.
 **     All rights reserved. 
 **
 **     当社の許可なしに複製および転載を禁ず
 **
 */


/*=========================================================*/
/*               Special Macro Definition                  */
/*=========================================================*/

#ifndef _INCLUDE_LIB_KM_NETAPI_H_
#define _INCLUDE_LIB_KM_NETAPI_H_

#include "librasp-netapi.h"

//<<included>> "librasp-netapi.h"
//#ifdef WIN32
//#ifdef RASP_EXPORTS
//#define _EXPORT __declspec(dllexport)
//#else // RASP_EXPORTS
//#define _EXPORT __declspec(dllimport)
//#endif // RASP_EXPORTS
//#define CONV_INET_ADDR_MODE
//#else // CONV_INET_ADDR_MODE
//#define _EXPORT extern
//#undef  CONV_INET_ADDR_MODE
//#endif // WIN32
/*=========================================================*/
/*                   Include Files                         */
/*=========================================================*/

//<<included>> "librasp-netapi.h"
//#ifdef WIN32
//#include <winsock.h>
//#else
//#include <sys/socket.h>
//#include <netinet/in.h>
//#endif

////#include "km-netapi-arg.h"
#ifndef _INCLUDE_KM_NET_API_COMMON_ARG_H_
#define _INCLUDE_KM_NET_API_COMMON_ARG_H_
// チャネル数
#define KM_WL_MAX_CH_NUM            4
// サンプリングレート
//<<included>> "rasp-netapi-arg.h"
//#define CLOCK_FREQ_11_025K          11025
//#define CLOCK_FREQ_16K              16000
//#define CLOCK_FREQ_22_05K           22050
//#define CLOCK_FREQ_32K              32000
//#define CLOCK_FREQ_44_1K            44100
//#define CLOCK_FREQ_48K              48000
//#define CLOCK_FREQ_64K              64000
//#define CLOCK_FREQ_EXT_SYNC         0
// データフォーマット
//<<included>> "rasp-netapi-arg.h"
//#define DATA_FORMAT_SYSTEM_DEFAULT  0
//#define DATA_FORMAT_FLOAT           1
//#define DATA_FORMAT_32BIT_PCM       0x10
//#define DATA_FORMAT_24BIT_PCM       0x11
//#define DATA_FORMAT_16BIT_PCM       0x12
// A/D同期サンプルCH数用
//<<included>> "librasp-netapi.h"
//#define AD_16CH_MODE                0    // CH0 - CH15同期サンプリング
//#define AD_8CH_MODE                 1    // CH0 - CH 7同期サンプリング
#define AD_4CH_MODE                 2    // CH0 - CH3同期サンプリング
//#define AD_CUSTOM_CH_MODE           0xF  /* 下記AD_CH*_CAPTUREによる任意指定 */
// チャネル単位で取り込みを有効にするフラグ
//<<included>> "rasp-netapi-arg.h"
//#define AD_CH0_CAPTURE              (1 <<  0)
//#define AD_CH1_CAPTURE              (1 <<  1)
//#define AD_CH2_CAPTURE              (1 <<  2)
//#define AD_CH3_CAPTURE              (1 <<  3)
//#define AD_CH4_CAPTURE              (1 <<  4)
//#define AD_CH5_CAPTURE              (1 <<  5)
//#define AD_CH6_CAPTURE              (1 <<  6)
//#define AD_CH7_CAPTURE              (1 <<  7)
//#define AD_CH8_CAPTURE              (1 <<  8)
//#define AD_CH9_CAPTURE              (1 <<  9)
//#define AD_CH10_CAPTURE             (1 << 10)
//#define AD_CH11_CAPTURE             (1 << 11)
//#define AD_CH12_CAPTURE             (1 << 12)
//#define AD_CH13_CAPTURE             (1 << 13)
//#define AD_CH14_CAPTURE             (1 << 14)
//#define AD_CH15_CAPTURE             (1 << 15)
// A/Dコンバータの動作モード
//<<included>> "rasp-netapi-arg.h"
//#define AD_LOW_POWER_MODE           0
//#define AD_HIGH_RESOLUTION_MODE     1
//#define AD_HIGH_SPEED_MODE          2 /* No-Used */
// A/D停止中のコンバータ状態
//<<included>> "rasp-netapi-arg.h"
//#define AD_SLEEP_MODE_OFF           0
//#define AD_SLEEP_MODE_ON            1
// 外部同期用クロック用
//<<included>> "rasp-netapi-arg.h"
//#define WORD_CLOCK_DISABLE          0
//#define WORD_CLOCK_ENABLE           1
// D/Aチャネルモード
//<<included>> "rasp-netapi-arg.h"
//#define DA_MONORAL_MODE             1 /* ホストPCからボード単位にデータを与え2chに分配 */
//#define DA_STEREO_MODE              2 /* ホストPCからCHごとにデータを与えるモード      */
//#define DA_SINGLE_MODE              3 /* ホストPCから1ch分のデータを与え全てに分配     */

//<<included>> "rasp-netapi-arg.h"
//#define DA_OUTPUT_DISABLE           0
//#define DA_OUTPUT_ENABLE            1
// A/D-->D/Aループバック用
//<<included>> "rasp-netapi-arg.h"
//#define DA_L_CH                     0
//#define DA_R_CH                     1

//<<included>> "rasp-netapi-arg.h"
//#define DA_NORMAL_MODE              0
//#define DA_LOOP_BACK_MODE           1
// D/A Mute 制御
//<<included>> "rasp-netapi-arg.h"
//#define DA_MUTE_ON                  1
//#define DA_MUTE_OFF                 0
// D/A Over Sampling Rate
//<<included>> "rasp-netapi-arg.h"
//#define DA_OVER_SAMPLING_RATE_64    0
//#define DA_OVER_SAMPLING_RATE_128   1
#endif // _INCLUDE_KM_NET_API_COMMON_ARG_H_

/*=========================================================*/
/*                   Macro Definition                      */
/*=========================================================*/
//<<included>> "librasp-netapi.h"
//#ifdef WIN32
//#define write(a, b, c) send(a, b, c, 0)
//#define read(a, b, c)  recv(a, b, c, 0)
//#define close closesocket
//#define ioctl ioctlsocket
//#endif // WIN32

// 通信対象
#define TARGET_TYPE_DUMMY           0 // dummy
#define TARGET_TYPE_KM_WL           1 // KM

/*=========================================================*/
/*                 Structure Declaration                   */
/*=========================================================*/
//<<included>> "librasp-netapi.h"
//#ifdef WIN32
//typedef unsigned long in_addr_t;
//#else // WIN32
//typedef int SOCKET;
//#endif // WIN32

//<<included>> "librasp-netapi.h"
//typedef struct _WS_SOCKET_INFO_t_
//{
//    SOCKET             socket_fd;
//    struct sockaddr_in socket_addr;
//    int                socket_addr_size;
//} WS_SOCKET_INFO_t;


typedef struct _WS_KM_HANDLE_t_
{
    WS_SOCKET_INFO_t server_info;
    
#ifdef CONV_INET_ADDR_MODE
    in_addr_t        server_ip_addr;
#else
    struct in_addr   server_ip_addr;
#endif

    int              ch_mode;            // [共通] チャネル数に関する動作モード
    int              ch_num;             // [共通] ソフトウェア上でのチャネル数
    int              board_num;          // [共通] RASP24のボードスタック枚数
    int              max_ch_num;         // [共通] 最大チャネル数（ボード単位）
    int              header_flag;        // [A/D]  ヘッダの有効、無効フラグ
    int              header_count;       // [A/D]  A/D開始からのフレーム数
    int              header_size;        // [A/D]  ヘッダのバイト数
    int              enable_channels[1]; // [RASP24_MAX_BOARD]

    int              resolution_bit;    // [共通] 個々のデータサイズ
    int              sample_per_frame;  // [A/D]  フレーム中のサンプル数
    int              byte_per_sample;   // [A/D]  サンプルあたりのバイト数
    int              frame_size;        // [A/D]  フレームのバイト数
    int              ad_shift;          // [A/D]  16bitモードでのシフト量

    int              target_type;       // [共通] 通信相手の情報
    int              target_sub_type;   // [共通] 通信相手の追加情報
    
    int              data_format;       // [共通] データフォーマット
    float            gain;              // [共通] floatモードでのスケール情報
    void             *work_buffer;      // [共通] 作業用バッファ

    int              time_sec;          // [A/D]  最終取得フレームの時刻(sec)
    int              time_usec;         // [A/D]  最終取得フレームの時刻(usec)

#if 0
    int              da_l_loop_mode;    /* [D/A]  D/A L-chループ設定           */
    int              da_r_loop_mode;    /* [D/A]  D/A R-chループ設定           */
#endif

    unsigned short   crc16_table[256];  // [KM]   受信データ用CRCテーブル
    int              crc_check_enable;  // [KM]   受信データのCRCエラーチェック有効
    int              crc_err_count;     // [KM]   受信データのCRCエラーカウント
} WS_KM_HANDLE_t;


/*=========================================================*/
/*                    Private Valiable                     */
/*=========================================================*/

/*=========================================================*/
/*                    Public Valiable                      */
/*=========================================================*/

#ifdef __cplusplus
extern "C" {
#endif

    /* A/D関係 */
    _EXPORT WS_KM_HANDLE_t *WsKmHandleOpen(char *ip_addr);
    _EXPORT int WsKmHandleClose(WS_KM_HANDLE_t *h_ws);

    _EXPORT int WsKmGetSystemInfo(WS_KM_HANDLE_t *h_ws, int *target_type);
    _EXPORT int WsKmGetTargetType(WS_KM_HANDLE_t *h_ws, int *target_type);
    _EXPORT int WsKmGetTargetSubType(WS_KM_HANDLE_t *h_ws, int *target_sub_type);
    _EXPORT int WsKmGetBytePerSample(WS_KM_HANDLE_t *h_ws, int *byte_per_sample);
    _EXPORT int WsKmGetFrameSize(WS_KM_HANDLE_t *h_ws, int *frame_size);
    _EXPORT int WsKmGetHeaderSize(WS_KM_HANDLE_t *h_ws, int *header_size);
    _EXPORT int WsKmGetChannelNum(WS_KM_HANDLE_t *h_ws, int *channel_num);
    _EXPORT int WsKmGetResolutionBit(WS_KM_HANDLE_t *h_ws, int *resolution_bit);
    _EXPORT int WsKmGetBoardNum(WS_KM_HANDLE_t *h_ws, int *board_num);
    _EXPORT int WsKmHandleShow(WS_KM_HANDLE_t *h_ws);

    _EXPORT int WsKmSetSamplingFreq(WS_KM_HANDLE_t *h_ws, int freq);
    _EXPORT int WsKmGetSamplingFreq(WS_KM_HANDLE_t *h_ws, int *freq);
    _EXPORT int WsKmSetChannelMode(WS_KM_HANDLE_t *h_ws,  int channel_mode);
    _EXPORT int WsKmGetChannelMode(WS_KM_HANDLE_t *h_ws,  int *channel_mode);
    _EXPORT int WsKmSetCaptureChannels(WS_KM_HANDLE_t *h_ws, int board_no, int channels);
    _EXPORT int WsKmGetCaptureChannels(WS_KM_HANDLE_t *h_ws, int board_no, int *channels);
    _EXPORT int WsKmSetCaptureChannelList(WS_KM_HANDLE_t *h_ws, int *channel_list);
    _EXPORT int WsKmGetCaptureChannelList(WS_KM_HANDLE_t *h_ws, int *channel_list);

    _EXPORT int WsKmSetHeaderFlag(WS_KM_HANDLE_t *h_ws, int header_flag);
    _EXPORT int WsKmGetHeaderFlag(WS_KM_HANDLE_t *h_ws, int *header_flag);

    _EXPORT int WsKmADStart(WS_KM_HANDLE_t *h_ws);
    _EXPORT int WsKmADStop(WS_KM_HANDLE_t *h_ws);
    _EXPORT int WsKmAdDaStart(WS_KM_HANDLE_t *h_ws);
    
    _EXPORT int WsKmSetAdMode(WS_KM_HANDLE_t *h_ws, int ad_mode);
    _EXPORT int WsKmGetAdMode(WS_KM_HANDLE_t *h_ws, int *ad_mode);
    _EXPORT int WsKmSetAdSleepMode(WS_KM_HANDLE_t *h_ws, int sleep_mode);
    _EXPORT int WsKmGetAdSleepMode(WS_KM_HANDLE_t *h_ws, int *sleep_mode);
    
    _EXPORT int WsKmDataRead(WS_KM_HANDLE_t *h_ws, char *buffer, size_t read_req_size);
    _EXPORT int WsKmSampleDataRead(WS_KM_HANDLE_t *h_ws, char *buffer, size_t read_req_size);
    _EXPORT int (*WsKmFrameRead)(WS_KM_HANDLE_t *h_ws, char *buffer, size_t read_req_size);
    
    _EXPORT int WsKmSetAdDataFormat(WS_KM_HANDLE_t *h_ws, int format_type);
    _EXPORT int WsKmGetAdDataFormat(WS_KM_HANDLE_t *h_ws, int *format_type);
    _EXPORT int WsKmSetCaptureGain(WS_KM_HANDLE_t *h_ws, int channel, int gain);
    _EXPORT int WsKmGetCaptureGain(WS_KM_HANDLE_t *h_ws, int channel, int *gain);
    _EXPORT int WsKmSetCaptureGainList(WS_KM_HANDLE_t *h_ws, int *gain_list);
    _EXPORT int WsKmGetCaptureGainList(WS_KM_HANDLE_t *h_ws, int *gain_list);

    //// D/A関係
    // _EXPORT WS_KM_HANDLE_t *WsKmDaHandleOpen(char *ip_addr);
    // _EXPORT int WsKmDaHandleClose(WS_KM_HANDLE_t *h_ws);
    // 
    // _EXPORT int WsKmSetDaFreq(WS_KM_HANDLE_t *h_ws, int freq);
    // _EXPORT int WsKmGetDaFreq(WS_KM_HANDLE_t *h_ws, int *freq);
    // _EXPORT int WsKmDaReady(WS_KM_HANDLE_t *h_ws);
    // _EXPORT int WsKmDaStart(WS_KM_HANDLE_t *h_ws);
    // _EXPORT int WsKmDaStop(WS_KM_HANDLE_t *h_ws);
    // _EXPORT int WsKmDaStopWait(WS_KM_HANDLE_t *h_ws);
    // 
    // _EXPORT int WsKmGetDaStatus(WS_KM_HANDLE_t *h_ws);
    // 
    // _EXPORT int WsKmSetDaLoopBackChannel(WS_KM_HANDLE_t *h_ws, int da_ch, int ad_ch);
    // _EXPORT int WsKmGetDaLoopBackChannel(WS_KM_HANDLE_t *h_ws, int da_ch, int *ad_ch);
    // 
    // _EXPORT int WsKmSetDaMode(WS_KM_HANDLE_t *h_ws, int da_ch, int mode);
    // _EXPORT int WsKmGetDaMode(WS_KM_HANDLE_t *h_ws, int da_ch, int *mode);
    // 
    // _EXPORT int WsKmSetDaOutputEnable(WS_KM_HANDLE_t *h_ws, int da_ch, int flag);
    // _EXPORT int WsKmGetDaOutputEnable(WS_KM_HANDLE_t *h_ws, int da_ch, int *flag);
    // 
    // _EXPORT int WsKmSetDaChannelMode(WS_KM_HANDLE_t *h_ws, int mode);
    // _EXPORT int WsKmGetDaChannelMode(WS_KM_HANDLE_t *h_ws, int *mode);
    // _EXPORT int WsKmGetDaChannelNum(WS_KM_HANDLE_t *h_ws, int *ch_num);
    // 
    // _EXPORT int WsKmSetDaMute(WS_KM_HANDLE_t *h_ws, int da_ch, int mute_flag);
    // _EXPORT int WsKmGetDaMute(WS_KM_HANDLE_t *h_ws, int da_ch, int *mute_flag);
    // _EXPORT int WsKmSetDaGain(WS_KM_HANDLE_t *h_ws, int da_ch, int gain);
    // _EXPORT int WsKmGetDaGain(WS_KM_HANDLE_t *h_ws, int da_ch, int *gain);
    // 
    // _EXPORT int (*WsKmDaDataWrite)(WS_KM_HANDLE_t *h_ws, char *buffer, size_t write_req_size);
    // _EXPORT int WsKmSetDaDataFormat(WS_KM_HANDLE_t *h_ws, int format_type);
    // _EXPORT int WsKmGetDaDataFormat(WS_KM_HANDLE_t *h_ws, int *format_type);
    // _EXPORT int WsKmDaSetWriteTimeout(WS_KM_HANDLE_t *h_ws, int timeout_sec);
    
    /* Word Clock関係 */
    _EXPORT int WsKmWordClockOutput(WS_KM_HANDLE_t *h_ws, int mode);

    /* 電源OFF */
    _EXPORT int WsKmSystemPowerDown(WS_KM_HANDLE_t *h_ws);

    //// RASP-LC無線 関係
    // _EXPORT WS_KM_HANDLE_t *WsKmLcHandleOpen(char *ip_addr);
    // _EXPORT int WsKmLcHandleClose(WS_KM_HANDLE_t *h_ws);
    // 
    // _EXPORT int WsKmLcGetFrameSize(WS_KM_HANDLE_t *h_ws, int *frame_size);
    // 
    // _EXPORT int WsKmLcADStart(WS_KM_HANDLE_t *h_ws);
    // _EXPORT int WsKmLcADStop(WS_KM_HANDLE_t *h_ws);
    // 
    // _EXPORT int WsKmLcDataRead(WS_KM_HANDLE_t *h_ws, char *buffer, size_t read_req_size);
#ifdef __cplusplus
}
#endif

#endif // _INCLUDE_LIB_KM_NETAPI_H_

/* EOF */
