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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Start with [`Handle::new`]

use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw::c_char;
use std::ptr::{null_mut, NonNull};

use rusb::{DeviceHandle, UsbContext};

use dir::{DirItem, DirList};
pub use error::*;
use info::Info;
use libnspire_sys::{
    free, nspire_attr, nspire_device_info, nspire_devinfo, nspire_dir_create, nspire_dirlist,
    nspire_file_copy, nspire_file_delete, nspire_file_move, nspire_file_read, nspire_file_write,
    nspire_free, nspire_handle, nspire_image, nspire_init, nspire_os_send, nspire_screenshot,
};

pub mod dir;
mod error;
pub mod info;

pub struct Handle<T: UsbContext> {
    handle: NonNull<nspire_handle>,
    _device: DeviceHandle<T>,
}

impl<T: UsbContext> Handle<T> {
    pub fn new(device: DeviceHandle<T>) -> Result<Self> {
        let mut handle: *mut nspire_handle = null_mut();
        err(unsafe { nspire_init(&mut handle, device.as_raw() as _) })?;
        Ok(Handle {
            handle: NonNull::new(handle).ok_or(Error::NoDevice)?,
            _device: device,
        })
    }

    pub fn info(&self) -> Result<Info> {
        unsafe {
            let mut info: nspire_devinfo = mem::zeroed();
            err(nspire_device_info(self.handle.as_ptr(), &mut info))?;
            Ok(info.into())
        }
    }

    pub fn screenshot(&self) -> Result<Image> {
        unsafe {
            let mut image: *mut nspire_image = null_mut();
            err(nspire_screenshot(self.handle.as_ptr(), &mut image))?;
            let width = (*image).width;
            let height = (*image).height;
            let bbp = (*image).bbp;
            let len = (width as u32 * height as u32 * bbp as u32) / 8;
            let data = (*image).data.as_slice(len as usize).into();
            free(image as _);
            Ok(Image {
                width,
                height,
                bbp,
                data,
            })
        }
    }

    pub fn move_file(&self, src: &str, dest: &str) -> Result<()> {
        let src = CString::new(src)?;
        let dest = CString::new(dest)?;
        unsafe {
            err(nspire_file_move(
                self.handle.as_ptr(),
                src.as_ptr(),
                dest.as_ptr(),
            ))
        }
    }

    pub fn file_attr(&self, src: &str) -> Result<DirItem> {
        let src = CString::new(src)?;
        unsafe {
            let mut item = mem::zeroed();
            err(nspire_attr(self.handle.as_ptr(), src.as_ptr(), &mut item))?;
            Ok(item.into())
        }
    }

    pub fn copy_file(&self, src: &str, dest: &str) -> Result<()> {
        let src = CString::new(src)?;
        let dest = CString::new(dest)?;
        unsafe {
            err(nspire_file_copy(
                self.handle.as_ptr(),
                src.as_ptr(),
                dest.as_ptr(),
            ))
        }
    }

    pub fn delete_file(&self, path: &str) -> Result<()> {
        let path = CString::new(path)?;
        unsafe { err(nspire_file_delete(self.handle.as_ptr(), path.as_ptr())) }
    }

    pub fn read_file(&self, path: &str, buf: &mut [u8]) -> Result<usize> {
        let path = CString::new(path)?;
        let mut bytes = 0;
        unsafe {
            err(nspire_file_read(
                self.handle.as_ptr(),
                path.as_ptr(),
                buf.as_mut_ptr() as _,
                buf.len() as _,
                &mut bytes,
            ))?;
        }
        Ok(bytes as usize)
    }

    pub fn write_file(&self, path: &str, buf: &[u8]) -> Result<()> {
        let path = CString::new(path)?;
        unsafe {
            err(nspire_file_write(
                self.handle.as_ptr(),
                path.as_ptr(),
                buf.as_ptr() as _,
                buf.len() as _,
            ))
        }
    }

    pub fn send_os(&self, buf: &[u8]) -> Result<()> {
        unsafe {
            err(nspire_os_send(
                self.handle.as_ptr(),
                buf.as_ptr() as _,
                buf.len() as _,
            ))
        }
    }

    pub fn create_dir(&self, path: &str) -> Result<()> {
        let path = CString::new(path)?;
        unsafe { err(nspire_dir_create(self.handle.as_ptr(), path.as_ptr())) }
    }

    pub fn delete_dir(&self, path: &str) -> Result<()> {
        let path = CString::new(path)?;
        unsafe { err(nspire_dir_create(self.handle.as_ptr(), path.as_ptr())) }
    }

    pub fn list_dir(&self, path: &str) -> Result<DirList> {
        let path = CString::new(path)?;
        unsafe {
            let mut list = null_mut();
            err(nspire_dirlist(
                self.handle.as_ptr(),
                path.as_ptr(),
                &mut list,
            ))?;
            Ok(DirList::from_raw(list))
        }
    }
}

impl<T: UsbContext> Drop for Handle<T> {
    fn drop(&mut self) {
        unsafe { nspire_free(self.handle.as_ptr()) }
    }
}

pub struct Image {
    pub width: u16,
    pub height: u16,
    pub bbp: u8,
    pub data: Vec<u8>,
}

unsafe fn c_str(s: &[c_char]) -> String {
    CStr::from_ptr(s.as_ptr()).to_string_lossy().to_string()
}