aboutsummaryrefslogtreecommitdiff
path: root/libvoltronic/voltronic_dev_impl.h
blob: 287734d8017cc33834b7fbe1acb477e502480bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * Copyright (C) 2019  Johan van der Vyver
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __VOLTRONIC__DEV__IMPL__H__
#define __VOLTRONIC__DEV__IMPL__H__

  /**
  * ------------------------------------------------------------------
   * ------------------------------------------------------------------
   * Used internally by implementations of voltronic_dev.h
   * Don't include unless you are building an implementation
   * ------------------------------------------------------------------
   * ------------------------------------------------------------------
   */

  #include "voltronic_dev.h"

  /**
   * Read up to buffer_size bytes from the device
   *
   * impl_ptr -> The underlying implementation's device pointer
   * buffer -> The buffer to store data from the device
   * buffer_size -> The maximum number of bytes to read
   * timeout_milliseconds -> Number of milliseconds before giving up
   *
   * Return the number of bytes successfully read from the device.
   * On failure returns < 0
   *
   * On failure set the appropriate error using SET_LAST_ERROR
   */
  int voltronic_dev_impl_read(
    void* impl_ptr,
    char* buffer,
    const size_t buffer_size,
    const unsigned int timeout_milliseconds);

  /**
   * Write the provided buffer data to the device
   *
   * impl_ptr -> The underlying implementation's device pointer
   * buffer -> The data to write to the device
   * buffer_size -> Number of bytes to write to the device
   * timeout_milliseconds -> Number of milliseconds before giving up
   *
   * Return the number of bytes successfully written to the device.
   * On failure returns < 0
   *
   * On failure set the appropriate error using SET_LAST_ERROR
   */
  int voltronic_dev_impl_write(
    void* impl_ptr,
    const char* buffer,
    const size_t buffer_size,
    const unsigned int timeout_milliseconds);

  /**
   * Accept the implementation pointer and close the underlying device connection
   *
   * Returns 0 on failure, anything else is considered success
   *
   * On failure set the appropriate error using SET_LAST_ERROR
   */
  int voltronic_dev_impl_close(
    void* impl_ptr);

  /**
   * Create the opaque pointer representing a connection to a physical voltronic device
   *
   * impl_ptr -> The underlying implementation's device pointer
   *
   * On failure sets the appropriate error using SET_LAST_ERROR
   */
  voltronic_dev_t voltronic_dev_internal_create(
    void* impl_ptr);

  /**
   * May change if operating system requires it.
   */
  #define ALLOCATE_MEMORY(__size__) \
    malloc(((size_t) (__size__)))

  #define COPY_MEMORY(__destination__, __source__, __size__) \
    memcpy(((void*) (__destination__)), \
      ((const void*) (__source__)), \
      ((size_t) (__size__)))

  #define FREE_MEMORY(__ptr__) \
    free(((void*) (__ptr__)))

  #if defined(_WIN32) || defined(WIN32)
    #include "windows.h"

    typedef DWORD last_error_t;

    #define SET_LAST_ERROR(_last_error_value_)  SetLastError((_last_error_value_))
    #define GET_LAST_ERROR()                    GetLastError()
    #define SET_INVALID_INPUT()                 SET_LAST_ERROR(ERROR_INVALID_DATA)

  #else

    #include <errno.h>

    typedef int last_error_t;

    #define SET_LAST_ERROR(_errno__value_)  errno = (_errno__value_)
    #define GET_LAST_ERROR()                (errno)
    #define SET_INVALID_INPUT()             SET_LAST_ERROR(EINVAL)

  #endif

#endif