/************************************************************
 **
 **     Object name : librasp-netapi.h
 **
 **     Description
 **     -- -- -- -- -- -- -- --
 **     RASPをネットワーク経由で利用するためのAPIのヘッダファイル
 **
 **     History
 **     -- -- -- -- -- -- -- --
 **     [ver2履歴]
 **     2012.12.06 K.Takagi RASP-LC無線版を取り込み
 **
 **     2011.04.21 K.Takagi WsSetAdSleepModeを追加
 **                         WsGetAdSleepModeを追加
 **     2011.04.12 K.Takagi 統合化のために名称を整理
 **
 **
 **     [ver1履歴]
 **     2011.02.07 K.Takagi コメント類の整備
 **     2011.02.02 K.Takagi Windows用マクロの名称を変更
 **     2010.10.16 K.Takagi 新規作成
 **                         従来のシステムをベースに移植
 **
 **
 **     Copyright (c) 2010 - 2011
 **     System In Frontier,Inc.
 **     All rights reserved. 
 **
 **     当社の許可なしに複製および転載を禁ず
 **
 */


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

#ifndef _INCLUDE_LIB_RASP_NETAPI_H_
#define _INCLUDE_LIB_RASP_NETAPI_H_

#ifdef WIN32

#ifdef RASP_EXPORTS
#define _EXPORT __declspec(dllexport)
#else
#define _EXPORT __declspec(dllimport)
#endif /* RASP_EXPORTS */

#define CONV_INET_ADDR_MODE

#else

#define _EXPORT extern

#undef  CONV_INET_ADDR_MODE

#endif
/*=========================================================*/
/*                   Include Files                         */
/*=========================================================*/

#ifdef WIN32

#include <winsock.h>

#else

#include <sys/socket.h>
#include <netinet/in.h>

#endif

#include "rasp-netapi-arg.h"

/*=========================================================*/
/*                   Macro Definition                      */
/*=========================================================*/
#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

/* 通信対象 */
#define TARGET_TYPE_RASP_WL         1 /* 無線RASP */
#define TARGET_TYPE_RASP24          4 /* RASP-24  */
#define TARGET_TYPE_RASPZX          5 /* RASP-ZX  */

#define RASP24_MAX_BOARD           16

/*=========================================================*/
/*                 Structure Declaration                   */
/*=========================================================*/
#ifdef WIN32
typedef unsigned long in_addr_t;
#else
typedef int SOCKET;
#endif

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_HANDLE_t_
{
    WS_SOCKET_INFO_t control_server_info;
    WS_SOCKET_INFO_t data_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[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

} WS_HANDLE_t;


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

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

#ifdef __cplusplus
extern "C" {
#endif

    /* A/D関係 */
    _EXPORT WS_HANDLE_t *WsHandleOpen(char *ip_addr);
    _EXPORT int WsHandleClose(WS_HANDLE_t *h_ws);

    _EXPORT int WsGetSystemInfo(WS_HANDLE_t *h_ws, int *target_type);
    _EXPORT int WsGetTargetType(WS_HANDLE_t *h_ws, int *target_type);
    _EXPORT int WsGetTargetSubType(WS_HANDLE_t *h_ws, int *target_sub_type);
    _EXPORT int WsGetBytePerSample(WS_HANDLE_t *h_ws, int *byte_per_sample);
    _EXPORT int WsGetFrameSize(WS_HANDLE_t *h_ws, int *frame_size);
    _EXPORT int WsGetHeaderSize(WS_HANDLE_t *h_ws, int *header_size);
    _EXPORT int WsGetChannelNum(WS_HANDLE_t *h_ws, int *channel_num);
    _EXPORT int WsGetResolutionBit(WS_HANDLE_t *h_ws, int *resolution_bit);
    _EXPORT int WsGetBoardNum(WS_HANDLE_t *h_ws, int *board_num);
    _EXPORT int WsHandleShow(WS_HANDLE_t *h_ws);

    _EXPORT int WsSetSamplingFreq(WS_HANDLE_t *h_ws, int freq);
    _EXPORT int WsGetSamplingFreq(WS_HANDLE_t *h_ws, int *freq);
    _EXPORT int WsSetChannelMode(WS_HANDLE_t *h_ws,  int channel_mode);
    _EXPORT int WsGetChannelMode(WS_HANDLE_t *h_ws,  int *channel_mode);
    _EXPORT int WsSetCaptureChannels(WS_HANDLE_t *h_ws, int board_no, int channels);
    _EXPORT int WsGetCaptureChannels(WS_HANDLE_t *h_ws, int board_no, int *channels);
    _EXPORT int WsSetCaptureChannelList(WS_HANDLE_t *h_ws, int *channel_list);
    _EXPORT int WsGetCaptureChannelList(WS_HANDLE_t *h_ws, int *channel_list);

    _EXPORT int WsSetHeaderFlag(WS_HANDLE_t *h_ws, int header_flag);
    _EXPORT int WsGetHeaderFlag(WS_HANDLE_t *h_ws, int *header_flag);

    _EXPORT int WsADStart(WS_HANDLE_t *h_ws);
    _EXPORT int WsADStop(WS_HANDLE_t *h_ws);
    _EXPORT int WsAdDaStart(WS_HANDLE_t *h_ws);
    
    _EXPORT int WsSetAdMode(WS_HANDLE_t *h_ws, int ad_mode);
    _EXPORT int WsGetAdMode(WS_HANDLE_t *h_ws, int *ad_mode);
    _EXPORT int WsSetAdSleepMode(WS_HANDLE_t *h_ws, int sleep_mode);
    _EXPORT int WsGetAdSleepMode(WS_HANDLE_t *h_ws, int *sleep_mode);
    
    _EXPORT int WsDataRead(WS_HANDLE_t *h_ws, char *buffer, size_t read_req_size);
    _EXPORT int WsSampleDataRead(WS_HANDLE_t *h_ws, char *buffer, size_t read_req_size);
    _EXPORT int (*WsFrameRead)(WS_HANDLE_t *h_ws, char *buffer, size_t read_req_size);
    
    _EXPORT int WsSetAdDataFormat(WS_HANDLE_t *h_ws, int format_type);
    _EXPORT int WsGetAdDataFormat(WS_HANDLE_t *h_ws, int *format_type);
    _EXPORT int WsSetCaptureGain(WS_HANDLE_t *h_ws, int channel, int gain);
    _EXPORT int WsGetCaptureGain(WS_HANDLE_t *h_ws, int channel, int *gain);
    _EXPORT int WsSetCaptureGainList(WS_HANDLE_t *h_ws, int *gain_list);
    _EXPORT int WsGetCaptureGainList(WS_HANDLE_t *h_ws, int *gain_list);

    
    /* D/A関係 */
    _EXPORT WS_HANDLE_t *WsDaHandleOpen(char *ip_addr);
    _EXPORT int WsDaHandleClose(WS_HANDLE_t *h_ws);
    
    _EXPORT int WsSetDaFreq(WS_HANDLE_t *h_ws, int freq);
    _EXPORT int WsGetDaFreq(WS_HANDLE_t *h_ws, int *freq);
    _EXPORT int WsDaReady(WS_HANDLE_t *h_ws);
    _EXPORT int WsDaStart(WS_HANDLE_t *h_ws);
    _EXPORT int WsDaStop(WS_HANDLE_t *h_ws);
    _EXPORT int WsDaStopWait(WS_HANDLE_t *h_ws);
    
    _EXPORT int WsGetDaStatus(WS_HANDLE_t *h_ws);

    _EXPORT int WsSetDaLoopBackChannel(WS_HANDLE_t *h_ws, int da_ch, int ad_ch);
    _EXPORT int WsGetDaLoopBackChannel(WS_HANDLE_t *h_ws, int da_ch, int *ad_ch);

    _EXPORT int WsSetDaMode(WS_HANDLE_t *h_ws, int da_ch, int mode);
    _EXPORT int WsGetDaMode(WS_HANDLE_t *h_ws, int da_ch, int *mode);

    _EXPORT int WsSetDaOutputEnable(WS_HANDLE_t *h_ws, int da_ch, int flag);
    _EXPORT int WsGetDaOutputEnable(WS_HANDLE_t *h_ws, int da_ch, int *flag);

    _EXPORT int WsSetDaChannelMode(WS_HANDLE_t *h_ws, int mode);
    _EXPORT int WsGetDaChannelMode(WS_HANDLE_t *h_ws, int *mode);
    _EXPORT int WsGetDaChannelNum(WS_HANDLE_t *h_ws, int *ch_num);

    _EXPORT int WsSetDaMute(WS_HANDLE_t *h_ws, int da_ch, int mute_flag);
    _EXPORT int WsGetDaMute(WS_HANDLE_t *h_ws, int da_ch, int *mute_flag);
    _EXPORT int WsSetDaGain(WS_HANDLE_t *h_ws, int da_ch, int gain);
    _EXPORT int WsGetDaGain(WS_HANDLE_t *h_ws, int da_ch, int *gain);

    _EXPORT int (*WsDaDataWrite)(WS_HANDLE_t *h_ws, char *buffer, size_t write_req_size);
    _EXPORT int WsSetDaDataFormat(WS_HANDLE_t *h_ws, int format_type);
    _EXPORT int WsGetDaDataFormat(WS_HANDLE_t *h_ws, int *format_type);
    _EXPORT int WsDaSetWriteTimeout(WS_HANDLE_t *h_ws, int timeout_sec);
    
    /* Word Clock関係 */
    _EXPORT int WsWordClockOutput(WS_HANDLE_t *h_ws, int mode);

    /* 電源OFF */
    _EXPORT int WsSystemPowerDown(WS_HANDLE_t *h_ws);


    /* RASP-LC無線 関係 */
    _EXPORT WS_HANDLE_t *WsLcHandleOpen(char *ip_addr);
    _EXPORT int WsLcHandleClose(WS_HANDLE_t *h_ws);
    
    _EXPORT int WsLcGetFrameSize(WS_HANDLE_t *h_ws, int *frame_size);
    
    _EXPORT int WsLcADStart(WS_HANDLE_t *h_ws);
    _EXPORT int WsLcADStop(WS_HANDLE_t *h_ws);
    
    _EXPORT int WsLcDataRead(WS_HANDLE_t *h_ws, char *buffer, size_t read_req_size);


#ifdef __cplusplus
}
#endif

#endif /* _INCLUDE_LIB_RASP_NETAPI_H_ */

/* EOF */
